/*
struct TreeNode {
int val;
struct TreeNode *left;
struct TreeNode *right;
TreeNode(int x) :
val(x), left(NULL), right(NULL) {
}
};*/
//本题深搜即可
class Solution {
public:
int count=0;//收集当前路径上的和
vector<vector<int >>res;//结果数组
vector<int> path;//路径数组
void dfs(TreeNode* root,int target){
count+=root->val;
path.push_back(root->val);
if(!root->left&&!root->right){//遇到叶子结点了
if(count==target){
res.push_back(path);
}
return;
}
if(root->left){//左子树不为空
dfs(root->left, target);
count-=root->left->val;//回溯
path.pop_back();//回溯
}
if(root->right){//右子树不为空
dfs(root->right, target);
count-=root->right->val;//回溯
path.pop_back();//回溯
}
return;
}
vector<vector<int>> FindPath(TreeNode* root,int expectNumber) {
if(root==NULL)
return res;
dfs(root, expectNumber);
return res;
}
};
struct TreeNode {
int val;
struct TreeNode *left;
struct TreeNode *right;
TreeNode(int x) :
val(x), left(NULL), right(NULL) {
}
};*/
//本题深搜即可
class Solution {
public:
int count=0;//收集当前路径上的和
vector<vector<int >>res;//结果数组
vector<int> path;//路径数组
void dfs(TreeNode* root,int target){
count+=root->val;
path.push_back(root->val);
if(!root->left&&!root->right){//遇到叶子结点了
if(count==target){
res.push_back(path);
}
return;
}
if(root->left){//左子树不为空
dfs(root->left, target);
count-=root->left->val;//回溯
path.pop_back();//回溯
}
if(root->right){//右子树不为空
dfs(root->right, target);
count-=root->right->val;//回溯
path.pop_back();//回溯
}
return;
}
vector<vector<int>> FindPath(TreeNode* root,int expectNumber) {
if(root==NULL)
return res;
dfs(root, expectNumber);
return res;
}
};