sup noobs, I'm implementing a General Tree in java and I need a function for height.
I thought this one would work:
But it only gets the left most subtree's height. If the height on one of the subtrees to the right is greater, it doesn't count that for some reason....
Thanks.
I thought this one would work:
Code:
public static int height(TreeNode t){
if (t == null) return -1; //no height
if (isLeaf(t)) return 0; //if you can't go down or right anymore
else return 1 + Math.max(height(t.getFirstSub()), height(t.getNextSub()));
}
But it only gets the left most subtree's height. If the height on one of the subtrees to the right is greater, it doesn't count that for some reason....
Thanks.