JC said:
Sorry Jack,
But I fugure that it was understood what language I was using.
anyway...
I tried using a "inorder" function to get the levels.
in addition of using if statements to run trought the left and right side of
the tree.
The problem is when one side has multiple parents.
Your problem is still not a C++ problem but a problem in understanding
some algorithms.
example
(35)
/ \
(27) (42)
/ \
(25) (31)
/ \
(29) (33)
/
(28)
so, if I have an if statement where
int level = 0;
p = root;
if ( p)
{
level++;
}
if ( (p->left == null) && (p->right == null) )
{
//end of branch
cout <<"\n Levels are <<level;
}
Since a binary tree is a recursive data structure, often recursive
functions are a natural choice:
int Height( tree* pNode )
{
if( pNode == NULL ) // an empty tree has height
// 0 by definition
return 0;
int HeightLeft = Height( pNode->left ); // determine height of left subtree
int HeightRight = Height( pNode->right ); // determine height of right subtree
// The height of this tree is then the maximum of the left and right
// subtree. Add one for this node.
//
//
// 27
// / \
// 1 +- 25 31 +--
// / 2
// 29 +--
//
// The height of the left subtree equals 1, the height of the right
// subtree equals 2. Thus the height of the total tree equals the
// maximum of both values: 2 plus 1 -> 3
return max( HeightLeft, HeightRight ) + 1;
}