-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathBinaryTreeAlgorithms.h
441 lines (389 loc) · 7.89 KB
/
BinaryTreeAlgorithms.h
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
/************************************************************************/
/* chapter 5: 二叉树 Ye Hu 2017/03/11 */
/************************************************************************/
#include <iostream>
#include <vector>
#include <algorithm>
#include <stack>
#include <utility>
using std::vector;
using std::stack;
using std::pair;
// 树节点
class TreeNode
{
public:
TreeNode(int value, TreeNode* lp=nullptr, TreeNode* rp=nullptr) :
val{ value }, left{ lp }, right{rp}
{}
int val;
TreeNode* left;
TreeNode* right;
};
// 单链表节点
class ListNode
{
public:
ListNode(int value, ListNode* np) :
val{ value }, next{ np }
{}
int val;
ListNode* next;
};
class Solution_6
{
public:
// 迭代版本:前序遍历
vector<int> preorderTraversal(TreeNode* root)
{
vector<int> result;
stack<TreeNode*> s;
TreeNode* p = nullptr;
if (root != nullptr)
{
s.push(root);
}
while (!s.empty())
{
p = s.top();
s.pop();
result.push_back(p->val);
if (p->right != nullptr)
{
s.push(p->right);
}
if (p->left != nullptr)
{
s.push(p->left);
}
}
return result;
}
// 迭代版本:中序遍历
vector<int> inorderTraversal(TreeNode* root)
{
vector<int> result;
stack<TreeNode*> s;
TreeNode* p = root;
while (!s.empty() || p != nullptr)
{
if (p != nullptr)
{
s.push(p);
p = p->left;
}
else
{
p = s.top();
s.pop();
result.push_back(p->val);
p = p->right;
}
}
return result;
}
// 迭代版本:后序遍历
vector<int> postorderTraversal(TreeNode* root)
{
vector<int> result;
stack<TreeNode*> s;
TreeNode* p = root;
TreeNode* prev = nullptr;
do
{
while (p != nullptr)
{
s.push(p);
p = p->left;
}
prev = nullptr;
while (!s.empty())
{
p = s.top();
if (p->right == prev)
{
result.push_back(p->val);
s.pop();
prev = p;
}
else
{
p = p->right;
break;
}
}
} while (!s.empty());
return result;
}
// Level级遍历
vector<vector<int>> levelOrder(TreeNode* root)
{
vector<vector<int>> result;
_traversa(root, 1, result);
return result;
}
void _traversa(TreeNode* root, int level, vector<vector<int>>& result)
{
if (root == nullptr)
{
return;
}
if (level > result.size())
{
result.push_back(vector<int>());
}
result[level - 1].push_back(root->val);
_traversa(root->left, level + 1, result);
_traversa(root->right, level + 1, result);
}
// 判断是否是平衡树
bool isBalancedTree(TreeNode* root)
{
return balancedHeight(root) > 0;
}
// 如果是平衡树,那么就返回最大树高,否则返回-1
int balancedHeight(TreeNode* root)
{
if (root == nullptr)
{
return 0;
}
int left = balancedHeight(root->left);
int right = balancedHeight(root->right);
if (left < 0 || right < 0 || std::abs(left - right) > 1)
{
return -1;
}
return std::max(left, right) + 1;
}
// 将二叉树转换为链表结构 拉平
void flatten(TreeNode* root)
{
if (root == nullptr )
{
return;
}
flatten(root->right);
if (root->left != nullptr)
{
flatten(root->left);
TreeNode* p = root->left;
while (p->right != nullptr)
{
p = p->right;
}
p->right = root->right;
root->right = root->left;
}
root->left = nullptr;
}
// 将二叉树转换为链表结构 拉平 , 迭代版本
void flattenItr(TreeNode* root)
{
stack<TreeNode*> s;
if (root != nullptr)
{
s.push(root);
}
TreeNode* p = nullptr;
while (!s.empty())
{
p = s.top();
s.pop();
if (p->right != nullptr)
{
s.push(p->right);
}
if (p->left != nullptr)
{
s.push(p->left);
}
p->left = nullptr;
if (!s.empty())
{
p->right = s.top();
}
}
}
// 合法的二叉搜索树
bool isValidBST(TreeNode* root)
{
return isValidBST(root, INT_MIN, INT_MAX);
}
bool isValidBST(TreeNode* root, int lower, int upper)
{
if (root == nullptr)
{
return true;
}
return root->val > lower && root->val < upper &&
isValidBST(root->left, lower, root->val) &&
isValidBST(root->right, root->val, upper);
}
// 将排序数组转化为平衡二叉搜索树
TreeNode* sortedArrayToBST(vector<int>& arr)
{
return sortedArrayToBST(std::begin(arr), std::end(arr));
}
template<typename RandomAccessIterator>
TreeNode* sortedArrayToBST(RandomAccessIterator first, RandomAccessIterator last)
{
auto length = std::distance(first, last);
if (length <= 0)
{
return nullptr;
}
auto mid = std::next(first, length / 2);
TreeNode* root = new TreeNode{ *mid };
root->left = sortedArrayToBST(first, mid);
root->right = sortedArrayToBST(mid + 1, last);
return root;
}
// 将单链表(有序)转化为二叉搜索树
TreeNode* sortedListToBST(ListNode* head)
{
int len = 0;
ListNode* p = head;
while (p != nullptr)
{
++len;
p = p->next;
}
return sortedListToBST(head, 0, len);
}
TreeNode* sortedListToBST(ListNode*& node, int first, int last)
{
if (first >= last)
{
return nullptr;
}
int mid = first + (last - first) / 2;
// 先处理左子树
TreeNode* leftTree = sortedListToBST(node, first, mid);
TreeNode* parent = new TreeNode{ node->val, leftTree, nullptr };
node = node->next;
parent->right = sortedListToBST(node, mid + 1, last);
return parent;
}
// 寻找二叉树的最小深度
int minDepth(TreeNode* root)
{
return minDepth(root, false);
}
int minDepth(TreeNode* root, bool hasBrother)
{
if (root == nullptr)
{
return hasBrother ? INT_MAX : 0;
}
return 1 + std::min(minDepth(root->left, root->right != nullptr),
minDepth(root->right, root->left != nullptr));
}
// 迭代版本
int minDepthItr(TreeNode* root)
{
if (root == nullptr)
{
return 0;
}
int result = INT_MAX;
stack<pair<TreeNode*, int>> s;
s.push(pair<TreeNode*, int>{ root, 1 });
while (!s.empty())
{
TreeNode* p = s.top().first;
int depth = s.top().second;
s.pop();
if (p->left == nullptr && p->right == nullptr)
{
result = std::min(result, depth);
}
if (p->left != nullptr && result > depth)
{
s.push(pair<TreeNode*, int>{p->left, depth+1});
}
if (p->right != nullptr && result > depth)
{
s.push(pair<TreeNode*, int>{p->right, depth+1});
}
}
return result;
}
// 最大树深
int maxDepth(TreeNode* root)
{
if (root == nullptr)
{
return 0;
}
return 1 + std::max(maxDepth(root->left), maxDepth(root->right));
}
// 路径和是否存在
bool hasPathSum(TreeNode* root, int sum)
{
if (root == nullptr)
{
return false;
}
if (root->left == nullptr && root->right == nullptr)
{
return (sum - root->val) == 0;
}
return (hasPathSum(root->right, sum - root->val) ||
hasPathSum(root->left, sum - root->val));
}
// 路径和是否存在,并返回路径
vector<vector<int>> pathSum(TreeNode* root, int sum)
{
vector<vector<int>> result;
vector<int> cur;
pathSum(root, sum, cur, result);
return result;
}
void pathSum(TreeNode* root, int gap, vector<int>& cur,
vector<vector<int>>& result)
{
if (root == nullptr)
{
return;
}
cur.push_back(root->val);
if (root->left == nullptr && root->right == nullptr)
{
if (gap == root->val)
{
result.push_back(cur);
}
}
pathSum(root->left, gap - root->val, cur, result);
pathSum(root->right, gap - root->val, cur, result);
cur.pop_back(); // 重要
}
// 最大路径和:可以从任意节点开始,到任意节点结束
int maxPathSum(TreeNode* root)
{
int max_sum = INT_MIN;
dfs(root, max_sum);
return max_sum;
}
int dfs(TreeNode* root, int& max_sum)
{
if (root == nullptr)
{
return 0;
}
int l = dfs(root->left, max_sum);
int r = dfs(root->right, max_sum);
int sum = root->val;
if (l > 0)
{
sum += l;
}
if (r > 0)
{
sum += r;
}
max_sum = std::max(max_sum, sum);
return std::max(r, l) > 0 ? std::max(r, l) + root->val : root->val;
}
};