二叉树中和为某一值的路径

Desicription

输入一颗二叉树和一个整数,打印出二叉树中结点值的和为输入整数的所有路径。路径定义为从树的根结点开始往下一直到叶结点所经过的结点形成一条路径。

Solution

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
/*
struct TreeNode {
int val;
struct TreeNode *left;
struct TreeNode *right;
TreeNode(int x) :
val(x), left(NULL), right(NULL) {
}
};*/
class Solution {
vector<vector<int>> res;
vector<int> tmp;
private:
void Dfs(TreeNode* root, int expectNumber) {
if(root == nullptr) {
return ;
}
tmp.push_back(root->val);
expectNumber -= root->val;
if(expectNumber == 0 && root->left == nullptr && root->right == nullptr) {
res.push_back(tmp);
}
Dfs(root->left, expectNumber);
Dfs(root->right, expectNumber);
if(!tmp.empty()) {
tmp.pop_back();
}
}
public:
vector<vector<int>> FindPath(TreeNode* root,int expectNumber) {
Dfs(root, expectNumber);
return res;
}
};