Why My C++ code took 66ms?


  • 0
    H
    class Solution {
        vector<vector<int>> res;
        vector<int> item;
        
    public:
        vector<vector<int>> pathSum(TreeNode* root, int sum) {
            
            if(root == NULL) return res;
            
            if (root->left == NULL && root->right == NULL)
            {
                if (root->val == sum)
                {
                    item.push_back(root->val);
                    res.push_back(item);
                    item.pop_back();
                }
                return res;
            }
            
            item.push_back(root->val);
            
            if (root->left) pathSum(root->left, sum - root->val);
            if (root->right) pathSum(root->right, sum - root->val);
            
            item.pop_back();
            return res;
        }
    };
    

Log in to reply
 

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