Skip to content

Instantly share code, notes, and snippets.

@yangpeng-chn
Created June 14, 2019 15:19
Show Gist options
  • Save yangpeng-chn/e59047469b43101f51b4018f2be1fef4 to your computer and use it in GitHub Desktop.
Save yangpeng-chn/e59047469b43101f51b4018f2be1fef4 to your computer and use it in GitHub Desktop.
Tree DFS
void helper(TreeNode* root, vector<vector<int>> &res, vector<int>&vec, int sum){
if(!root)return;
vec.push_back(root->val);
if(!root->left && !root->right && root->val==sum)
res.push_back(vec);
helper(root->left, res, vec, sum-root->val);
helper(root->right, res, vec, sum-root->val);
vec.pop_back();
}
vector<vector<int>> pathSum(TreeNode* root, int sum) {
vector<vector<int>> res;
vector<int>vec;
helper(root, res, vec, sum);
return res;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment