Tree: Height of a Binary Tree

  • + 0 comments

    My Java solution w/ linear time and o(h) space:

    public static int height(Node root) {
            if(root == null) return -1; //no height
            
            int leftHeight = height(root.left);
            int rightHeight = height(root.right);
            
            return 1 + Math.max(leftHeight, rightHeight);
        }