Java 0ms - Recursive Back-tracking w/ immediate child elimination.


  • 0
    S
    public class Solution {
        public int maxDepth(TreeNode root) {
           // implement using elimiation of next children nodes that are null before recursive call. 
         if (root == null) return 0;
         else if (root.right != null && root.left != null) 
         return
                    Math.max ( maxDepth(root.right) + 1 ,   maxDepth(root.left) + 1); 
             
            else if (root.right != null) return maxDepth (root.right) + 1;
            else /* if (root.left != null) */ return maxDepth (root.left ) +1; 
            
        }
    }
    
    

Log in to reply
 

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