-
Notifications
You must be signed in to change notification settings - Fork 0
/
二叉树中和为某一值的路径.txt
81 lines (75 loc) · 2.46 KB
/
二叉树中和为某一值的路径.txt
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
/*
struct TreeNode {
int val;
struct TreeNode *left;
struct TreeNode *right;
TreeNode(int x) :
val(x), left(NULL), right(NULL) {
}
};*/
/**************************************
输入一颗二叉树和一个整数,打印出二叉树中结点值的和为输入整数的所有路径。
路径定义为从树的根结点开始往下一直到叶结点所经过的结点形成一条路径。
**************************************/
class Solution {
public:
//深度优先搜索
//利用形参不改变原来变量值的特性以及引用传参为地址传值的方式
//缺点:每次访问新节点temp都要拷贝一次
void dfs(TreeNode* root,vector<vector<int>>& result,vector<int> temp,int sum,int expectnumber){
if(root==NULL) return;
sum += root->val;
if(sum > expectnumber) return;
else if(sum < expectnumber){
temp.push_back(root->val);
dfs(root->left, result, temp, sum, expectnumber);
dfs(root->right, result, temp, sum, expectnumber);
}
else if(sum==expectnumber && root->left==NULL && root->right == NULL){
temp.push_back(root->val);
result.push_back(temp);
}
}
vector<vector<int> > FindPath(TreeNode* root,int expectNumber) {
vector<vector<int>> result;
vector<int> temp;
dfs(root,result,temp,0,expectNumber);
return result;
}
};
/*
struct TreeNode {
int val;
struct TreeNode *left;
struct TreeNode *right;
TreeNode(int x) :
val(x), left(NULL), right(NULL) {
}
};*/
class Solution {
public:
//深度优先搜索
//利用引用传参为地址传值的方式
void dfs(TreeNode* root,vector<vector<int>>& result,vector<int>& temp,int sum,int expectnumber){
if(root==NULL) return;
sum += root->val;
if(sum > expectnumber) return;
else if(sum < expectnumber){
temp.push_back(root->val);
dfs(root->left, result, temp, sum, expectnumber);
dfs(root->right, result, temp, sum, expectnumber);
temp.pop_back();
}
else if(sum==expectnumber && root->left==NULL && root->right == NULL){
temp.push_back(root->val);
result.push_back(temp);
temp.pop_back();
}
}
vector<vector<int> > FindPath(TreeNode* root,int expectNumber) {
vector<vector<int>> result;
vector<int> temp;
dfs(root,result,temp,0,expectNumber);
return result;
}
};