Where to improve upon (16ms solution)


  • 0
    K
    class Solution {
    private:
        vector<int> current_path;
        vector<vector<int>> result;
        int target;
        
        void dfs(TreeNode* root, int sum){
            if(!root) return;
            sum+=root->val;
            current_path.push_back(root->val);
            
            if(sum==target) if(!root->left && !root->right) result.push_back(current_path);
            
            dfs(root->left, sum);
            dfs(root->right, sum);
            current_path.pop_back();
        }
        
    public:
        vector<vector<int>> pathSum(TreeNode* root, int sum) {
            target=sum;
            dfs(root, 0);
            return result;
        }
    };

Log in to reply
 

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