Easy Java Soln


  • 1
    public class Solution {
        public int maxDepth(TreeNode root) {
            if (root == null) return 0;
            // if (root.right == null) return 1 + maxDepth(root.left);
            // if (root.left == null) return 1 + maxDepth(root.right);
            return 1 + Math.max(maxDepth(root.left), maxDepth(root.right)); //isLeaf is checked here
        }
    }
    

    If the root is null return 0;

    If the root is not null return the max of

    1 + the max depth of the left and
    1 + the max depth of the right


  • 0
    Z

    okey, my solution just same to you.
    it really easy and short.


Log in to reply
 

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