Why is my code so slow (12ms) even though I'm using the same alg as others

  • 0
    class Solution {
        bool hasPathSum(TreeNode* root, int sum) {
            if(root==NULL) return false;
            sum -= root->val;
            if(root->left==NULL && root->right==NULL && sum==0) return true;
            return hasPathSum(root->left, sum) || hasPathSum(root->right, sum);

    This looks like the same algorithm that others are using and people say that they are doing it in 1ms in Java, but why is mine taking 12ms?

Log in to reply

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