My easy to read recursive Java solution


  • 0
    T
    public class Solution {
        
        public int maxDepth(TreeNode root) {
            
            if (root == null) {
                return 0;
            }
            
            int leftDepth = maxDepth(root.left);
            int rightDepth = maxDepth(root.right);
            
            if (leftDepth >= rightDepth) {
                return ++leftDepth;
            }
            else {
                return ++rightDepth;
            }
            
        }
    }
    

Log in to reply
 

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