The same code, one is AC, one is TLE??


  • 0
    M
    #define MAX(a,b) (((a)>(b))?(a):(b))
    int maxDepth(struct TreeNode* root) {
        if(root == 0) return 0;
        int l = maxDepth(root->left);
        int r = maxDepth(root->right);
        return 1 + MAX( l, r);
    }
    

    This code can pass OJ's test. But if I change a little bit, it will show TLE.

    #define MAX(a,b) (((a)>(b))?(a):(b))
    int maxDepth(struct TreeNode* root) {
        if(!root) return 0;
        return 1 + MAX( maxDepth(root->left), maxDepth(root->right) );
    }
    

    I don't think they are much different. Can any one explain? Thank you!


Log in to reply
 

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