/* If the tree is empty */ if(node ==null) return-1;
/* Compute the depth of subtrees recursively */ int heightLeft = computeHeight(node.left); int heightRight = computeHeight(node.right);
/* Compare the height in the left and right subtree : return the maximum one*/ if(heightLeft > heightRight) return heightLeft +1; else return heightRight +1; }
Time complexity = \( O(n) \) where \( n \) is the number of nodes in the binary tree.