vector of the highest path in a bin-tree

Hi all,
I have a function signature: vector<int> tree_highest_path (Node *root);
which gets a pointer to a binary-tree root.(the struct is:[int id, Node *right, Node left].
I need to return a vector of id's of the highest path of the tree.
can you help me with that? example:
root:6, left of root:5, right of root:3, right of right:1
will return this vector: 6->3->1.

Thanks
Last edited on
> highest path of the tree
I'm going to read that as longest path to a leaf
1
2
3
4
5
6
7
8
9
10
path 
longest_path_to_a_leaf( Node *root ){
   if( root->is_leaf() ) return path( root->id );
   left = longest_path_to_a_leaf( root->left );
   right = longest_path_to_a_leaf( root->right );
   longest = right.length() > left.lenght()? right: left;

   longest.push( root->id )
   return longest;
}
Last edited on
Hi,
I can't understand your code.
What is this path in 1st line?
Where is the vector variable? (look at the function signature)
What are those left and right variables? You didnt define them. Same as longest.
What is path function?
What is length() function? vector doesnt support length() operation.
What is push() function?

p.s - this function should be written in C++ in case I haven't said it
Last edited on
> What is this path in 1st line?
a proper structure that would store the ids of the nodes, and support operations like adding an id and obtain the length.

> What are those left and right variables? You didnt define them. Same as longest.
They are paths
It can be deduced as they are assigned the result of the `longest_path...()' function

> What is path function?
Construct a path with only one id. If you prefer
1
2
path result;
result.push( root->id );


> this function should be written in C++ in case I haven't said it
http://www.gocomics.com/peanuts/1974/01/09
Topic archived. No new replies allowed.