Maximum Depth of Binary Tree Definition Problem


  • 0
    N

    At very first, I wrote

    if(root==null) return -1;
    

    from this question, however, the test case give me input:[], output:0
    which make me absolutely wrong. However, let's see the definition of depth of a tree from Wikipedia: The depth of a node is the number of edges from the tree's root node to the node.
    that mean from root to itself, this should be 0, and then if from null to a root, it should be -1, otherwise we have from root to itself is 1+max(0,0)=1 according to this program. I think this should be not correct. What do u think?

    Here is my solution:

    public class Solution {
        public int maxDepth(TreeNode root) {
            if(root==null) return -1;
            else return 1+Math.max(maxDepth(root.left), maxDepth(root.right));
        }
    }
    

Log in to reply
 

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