[Java] [Recursion] Simple and easy to understand solution


  • 1
    M

    Iterating through left and right nodes, calculating depth of each node and returning the biggest one.

    public int maxDepth(TreeNode root) {
        if (root == null) return 0;
    
        int left = 1 + maxDepth(root.left);
        int right = 1 + maxDepth(root.right);
    
        return left > right ? left : right;
    }
    

Log in to reply
 

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