Bad recursion of min Depth of a binary tree

  • -3

    //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 {
    //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;
    return minDepth(root->right) + 1;
    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.