Recursive cpp solution


  • 0
    C
    class Solution {
    public:
        int minDepth(TreeNode* root) 
        {
            if(!root) return 0;
            int lh = minDepth(root->left);
            int rh = minDepth(root->right);
         
            // be careful, it's the height to leaves
            
            if(!root->left) return 1+rh;
            if(!root->right) return 1+lh;
            return 1 + min(lh , rh);
        }
    };

Log in to reply
 

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