12 ms C++ code (~3 lines)


  • 0
    P
    int minDepth(TreeNode* root) {
        if(!root) return 0;
        if(!root->left||!root->right) return 1+max(minDepth(root->left),minDepth(root->right));
        return 1+min(minDepth(root->left),minDepth(root->right));
    }

  • 0
    T

    Nice, saving a bunch of ifs with a max :)


Log in to reply
 

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