4 line 13 ms c++ code


  • 13
    K

    some minor change to the recursive solution of the maximum depth problem

    class Solution {
    public:
        int minDepth(TreeNode *root) {
            if (!root) return 0;
            if (!root->left) return 1 + minDepth(root->right);
            else if (!root->right) return 1 + minDepth(root->left);
            return 1 + min(minDepth(root->left), minDepth(root->right));
        }
    };

Log in to reply
 

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