Height of Tree

Problem:

Find the height of binary tree.

Examples:

5

/ \

3 8

/ \ \

1 4 11

The height of above binary tree is 3.

How is the binary tree represented?

We use the level order traversal sequence with a special symbol "#" denoting the null node.

Solution: Tail Recursion

public int getHeight(TreeNode root){
    if ( root == null) return 0;
    return Math.max(getHeight(root.left), getHeight(root.right)) + 1;
}

Last updated

Was this helpful?