My C++ solution. Any improvement ?


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

Log in to reply
 

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