Clean recursive java solution

  • 0

    The idea is start at root node with count 0, recursively goes down. At each recursive call, the count add 1. And return the max count.

    public int maxDepth(TreeNode root) {
    if(root == null ) return 0;
    return count(root,0);

    private int count(TreeNode node, int count ){
        if( node == null ) return count;        
        return Math.max(count(node.left,count+1),count(node.right,count+1));

Log in to reply

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