I need to write a method that determines whether a binary tree is balanced. So first I'm going to have to determine the height of each side of the tree. But I'm having trouble understanding how I am going to count the max length of a subtree, without counting all the nodes in the subtree. This question is very difficult to ask for me, so you guys can understand.
// primary method
public int Height()
{
int h = height( root );
}
// recursive method
private int height( Node x )
{
if( x == null ) return 0;
count++;
height( x.left );
height( x.right );
return count;
}
Here is my code for calculating the max height of the tree. But i dont know how to determine just the left or right sides' height, and this method seems to count the amount of nodes in the tree itself.