Created
June 14, 2019 15:19
-
-
Save yangpeng-chn/e59047469b43101f51b4018f2be1fef4 to your computer and use it in GitHub Desktop.
Tree DFS
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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