Simple 3ms C++ solution with explainations


  • 0
    M

    Pretty straight forward.
    Base case is when root is NULL.
    Else, for all just increment the height by one.
    Timing varies ard 3ms. I submitted multiple times of the same code.

    Hope it helps! :)

        int maxDepth(TreeNode* root) {
            
            if(!root)
                return 0;
            
            int maxL = maxDepth(root->left);
            int maxR = maxDepth(root->right);
            
            return (maxL > maxR) ? maxL+1 : maxR+1);
        }
    

Log in to reply
 

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