3 lines of solution c++


  • 0
    P
        int maxDepth(TreeNode *root) {
            return helper(root,0);
        }
        
        int helper(TreeNode* root, int d) {
            if (!root) return d;
            return max(helper(root->left, d+1), helper(root->right, d+1));
        }

  • 1
    Z
    class Solution {
    public:
        int maxDepth(TreeNode *root) {
            if(!root)
                return 0;
            return 1 + max(maxDepth(root->left), maxDepth(root->right));
        }
    };
    

    I guess we don't need a helper function to do this...


  • 0
    P

    Yes, you are correct! :)


Log in to reply
 

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