/**
* Definition for binary tree
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
vector<vector<int> > pathSum(TreeNode *root, int sum) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
vector<vector<int> > paths;
vector<int> curPath;
dfs(root, sum, paths, curPath);
return paths;
}
void dfs(TreeNode *root, int curRest, vector<vector<int> > &paths, vector<int> &curPath)
{
if (root == NULL)
return;
curPath.push_back(root->val);
if (root->left == NULL && root->right == NULL)
{
if (root->val == curRest)
paths.push_back(curPath);
}
else
{
dfs(root->left, curRest - root->val, paths, curPath);
dfs(root->right, curRest - root->val, paths, curPath);
}
curPath.pop_back();
}
};
LeetCode-Path Sum II
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.