My Java recursive solution (1 ms)


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

Log in to reply
 

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