C++ 7 lines DFS solution


  • 0
     int minDepth(TreeNode* root) {
            if(!root) return 0;
            if(!root->left && !root->right) return 1;
            int d_left=INT_MAX;
            int d_right=INT_MAX;
            if(root->left) d_left=minDepth(root->left)+1;
            if(root->right) d_right=minDepth(root->right)+1;
            return min(d_left,d_right);
        }
    

Log in to reply
 

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