/*
struct TreeNode {
int val;
struct TreeNode *left;
struct TreeNode *right;
TreeNode(int x) :
val(x), left(NULL), right(NULL) {
}
};*/
class Solution {
void DFS_Find_Path(TreeNode *root,int rest,vector<vector<int>> &path,vector<int> &ret){
rest -=root->val;
ret.push_back(root->val);
if(root->left==NULL&&root->right==NULL)
{
if(rest==0)
{
path.push_back(ret);
}
}
if(rest!=0&&root->left!=NULL)
{
DFS_Find_Path(root->left,rest,path,ret);
}
if(rest!=0&&root->right!=NULL)
{
DFS_Find_Path(root->right,rest,path,ret);
}
ret.pop_back();
};
public:
vector<vector<int> > FindPath(TreeNode* root,int expectNumber) {
vector<vector<int>> path;
vector<int> ret;
if(root!=NULL)
{
DFS_Find_Path(root,expectNumber,path,ret);
}
return path;
}
};
剑指offer 二叉树这种和为某一值的路径
最新推荐文章于 2022-05-16 10:18:03 发布