My solution using recursion


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

    };


Log in to reply
 

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