My Simple C++ Recursion Solution 16ms


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

Log in to reply
 

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