12 ms C++ Solution using recursion


  • 0
    H
    int minDepth(TreeNode* root) {
        if(root == NULL){
            return 0;
        }
        return findDepth(root);
    }
    int findDepth(TreeNode* root){
        if(root->left == NULL && root->right == NULL){
            return 1;
        }
        int left = INT_MAX;
        int right = INT_MAX;
        if(root->left != NULL){
            left = findDepth(root->left);
        }
        if(root->right != NULL){
           right  = findDepth(root->right); 
        }
        
        
        if(left<right){
            return left+1;
        }else{
            return right+1;
        }
    }

Log in to reply
 

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