-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path113.路径总和-ii.cpp
89 lines (80 loc) · 2.56 KB
/
113.路径总和-ii.cpp
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
82
83
84
85
86
87
88
/*
* @lc app=leetcode.cn id=113 lang=cpp
*
* [113] 路径总和 II
*/
// @lc code=start
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
// 方法一, 回溯算法
/*
class Solution {
public:
vector<vector<int>> ans;
vector<int> path;
void dfs(TreeNode * root, int sum){
if (!root->left && !root->right) {
if (sum == 0) ans.push_back(path);
return;
}
if (root->left) {
path.push_back(root->left->val);
sum -= root->left->val;
dfs(root->left, sum);
sum += root->left->val;
path.pop_back();
}
if (root->right){
path.push_back(root->right->val);
sum -= root->right->val;
dfs(root->right, sum);
sum += root->right->val;
path.pop_back();
}
}
vector<vector<int>> pathSum(TreeNode* root, int sum) {
if (root) {
path.push_back(root->val);
dfs(root, sum - root->val);
}
return ans;
}
};
*/
// 方法二,使用迭代。
// 采用后序遍历, 模拟回溯过程。
class Solution {
public:
vector<vector<int>> pathSum(TreeNode* root, int sum) {
vector<vector<int>> ans; vector<int> curr; //分别记录所有满足条件的路径、一条满足条件的路径
if (!root) return ans;
stack<TreeNode*> stk; TreeNode *prev = nullptr;
while (root || !stk.empty()) { //模拟系统递归栈
while (root) {
stk.push(root); sum -= root->val; curr.push_back(root->val); //入栈、更新剩余和、路径
root = root->left;
}//递归访问左结点
root = stk.top(); //不能再左了,取得右拐点(根结点)
if (!root->left && !root->right && (sum == 0)) { //条件:是叶子结点且剩余和为0
ans.push_back(curr); //满足条件,保存路径
}
if (!root->right || root->right == prev) { //右结点不存在或已经访问 回溯
stk.pop(); sum += root->val; curr.pop_back(); //出栈、更新剩余和、路径
prev = root; //标记已访问
root = nullptr; //用于回溯到上一级
}
else { //递归访问右结点 存在右边节点。
root = root->right;
}
}
return ans;
}
};
// @lc code=end