Bad recursion of min Depth of a binary tree


  • -3
    C

    //The problem is the minDepth(root->left)>=minDepth(root->right)
    //if I use minDepth(root->left)>minDepth(root->right), the result is right, otherwise the result is wrong.

    class Solution {
    public:
    //the problem
    int minDepth(TreeNode* root) {
    if(root == NULL)
    return 0;
    if(root->left == NULL)
    return minDepth(root->right)+1;
    if(root->right == NULL)
    return minDepth(root->left)+1;
    if(minDepth(root->left)>=minDepth(root->right)){
    return minDepth(root->right) + 1;
    }else{
    return minDepth(root->left) + 1;
    }
    }
    };


Log in to reply
 

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