Easiest 1ms Java solution


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

  • 0
    N

    why my code cost 8ms?
    It looks almost the same with yours, I use c++ and this is my code :

    int maxDepth(TreeNode* root) {
        if(root == NULL)
            return 0;
      
        int hL = maxDepth(root->left);
        int hR = maxDepth(root->right);
        
        return hL>hR?(hL+1):(hR+1);
    }
    

Log in to reply
 

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