Maximum Depth of Binary Tree Solution in Java


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

Log in to reply
 

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