What the "Time limit exceeded" means? Maximum Height (Depth) of a Binary Tree

  • 1
    public static int maxDepth(TreeNode root) {
        int max = 0;
        if(root == null)
            return 0;
        max = maxDepth(root.left) > maxDepth(root.right) ? maxDepth(root.left) : maxDepth(root.right);
        return max + 1;

  • 4

    Every time your call maxDepth() will hit a recursion.

    You need to save the value of maxDepth(root.left) and maxDepth(root.right), then do the comparison.

Log in to reply

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.