Share my c code


  • 1
    K

    int minDepth(struct 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 min(minDepth(root->left), minDepth(root->right)) + 1;
    

    }


  • 0
    C

    why "return min(minDepth(root->right), minDepth(root->left)) + 1;" exceeds time


  • 0
    E

    if you

    define min(A,B) ((A)<(B)?(A):(B))

    You should write in this way to avoid exceeding time:

    int lDepth = minDepth(root->left);

    int rDepth = minDepth(root->right);

    return min(lDepth, rDepth)+1;


Log in to reply
 

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