My 6 line easy-to-come-up-with C++ solution


  • 0
    X
    class Solution {
    public:
        int minDepth(TreeNode *root) {
            if (!root)      return 0;
            if (!root->left && !root->right)    return 1;
            
            int min_ = INT_MAX;
            if (root->left)     min_ = min(min_, minDepth(root->left) + 1);
            if (root->right)    min_ = min(min_, minDepth(root->right) + 1);
            return min_;
        }
    };
    

    I also posted an alternative in my blog, which is identical to someone's posted solution.


Log in to reply
 

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