-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path0112.cpp
155 lines (147 loc) · 3.09 KB
/
0112.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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
#include <iostream>
#include <vector>
#include <stack>
using namespace std;
struct TreeNode
{
int val;
TreeNode* left;
TreeNode* right;
TreeNode(int x) :val(x), left(NULL), right(NULL) {}
};
class Solution {
public:
/*
// 递归 先序遍历 中左右
bool hasPathSum(TreeNode* root, int targetSum)
{
vector<int> path;
if (root == NULL)
return false;
path.push_back(root->val);
return preorder(root, path, targetSum);
}
// 1. 确定参数和返回参数
bool preorder(TreeNode* root, vector<int>& path, int targetSum)
{
// 2. 确定终止条件
if (root->left == NULL && root->right == NULL)
{
int size = path.size();
int sum = 0;
for (int i = 0; i < size; i++)
{
sum += path[i];
}
if (sum == targetSum)
return true;
else
return false;
}
bool ans1 = false;
bool ans2 = false;
// 3. 单次递归处理
if (root->left != NULL)
{
path.push_back(root->left->val);
ans1 = preorder(root->left, path, targetSum);
path.pop_back(); // 回溯
}
if (root->right != NULL)
{
path.push_back(root->right->val);
ans2 = preorder(root->right, path, targetSum);
path.pop_back(); // 回溯
}
return ans1 || ans2;
}
*/
// 迭代 先序遍历 中左右
bool hasPathSum(TreeNode* root, int targetSum)
{
if (root == NULL)
return false;
stack<TreeNode*> st;
stack<vector<int>> pathSt;
vector<int> path;
path.push_back(root->val);
pathSt.push(path);
st.push(root);
int sum = 0;
while (!st.empty())
{
TreeNode* node = st.top(); // 中
st.pop();
path = pathSt.top();
pathSt.pop();
if (node->left == NULL && node->right == NULL)
{
sum = 0;
for (int i = 0; i < path.size(); i++)
{
sum += path[i];
}
if (sum == targetSum)
return true;
}
if(node->right != NULL)
{
st.push(node->right); // 右
vector<int> temp = path;
temp.push_back(node->right->val);
pathSt.push(temp);
}
if(node->left != NULL)
{
st.push(node->left); // 左
vector<int> temp = path;
temp.push_back(node->left->val);
pathSt.push(temp);
}
}
return false;
}
bool hasPathSum(TreeNode* root, int targetSum)
{
if (root == NULL)
return false;
return traversal(root, targetSum - root->val);
}
private:
bool traversal(TreeNode* cur, int count)
{
if (cur->left == NULL && cur->right == NULL && count == 0)
return true;
//2. 终止条件 遇到叶子节点返回
if (cur->left == NULL && cur->right == NULL)
return false;
if (cur->left != NULL) // 左
{
count -= cur->left->val; // 递归,处理节点
if (traversal(cur->left, count))
return true;
count += cur->left->val; // 回溯,撤销处理结果
}
if (cur->right) // 右
{
count -= cur->right->val; // 递归,处理节点
if (traversal(cur->right, count))
return true;
count += cur->right->val; // 回溯,撤销处理结果
}
return false;
}
};
int main()
{
TreeNode* root = new TreeNode(1);
TreeNode* node = root;
node->left = new TreeNode(2);
node->right = new TreeNode(3);
node->left->left = new TreeNode(4);
node->left->right = new TreeNode(5);
// node->right->right = new TreeNode(7);
Solution S;
S.hasPathSum(root, 4);
return 0;
}