-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path_6binaryTree.cpp
More file actions
749 lines (732 loc) · 24.3 KB
/
Copy path_6binaryTree.cpp
File metadata and controls
749 lines (732 loc) · 24.3 KB
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
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
#include <algorithm>
#include <cstdint>
#include <cstdlib>
#include <queue>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;
struct TreeNode {
int val;
TreeNode *left;
TreeNode *right;
TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
void traversal(TreeNode *cur, vector<int> &vec) {
if (cur == nullptr) return;
vec.push_back(cur->val);
traversal(cur->left, vec);
traversal(cur->right, vec);
}
};
// 前序遍历 递归法
class lc144 {
void traversal(TreeNode *cur, vector<int> &vec) {
if (cur == nullptr) return;
vec.push_back(cur->val);
traversal(cur->left, vec);
traversal(cur->right, vec);
}
public:
vector<int> preorderTraversal(TreeNode *root) {
vector<int> res;
traversal(root, res);
return res;
}
};
// 前序遍历 迭代法
class lc144_dd {
public:
vector<int> preorderTraversal(TreeNode *root) {
vector<int> res;
stack<TreeNode *> st;
if (root == nullptr) return vector<int>{};
st.push(root);
while (!st.empty()) {
TreeNode *node = st.top();
st.pop();
res.push_back(node->val);
if (node->right) { st.push(node->right); }
if (node->left) { st.push(node->left); }
}
return res;
}
};
// 中序遍历
class inorder {
public:
vector<int> inorderTraversal(TreeNode *root) {
vector<int> result;
stack<TreeNode *> st;
TreeNode *cur = root;
while (cur != nullptr || !st.empty()) {
if (cur != nullptr) {
st.push(cur);
cur = cur->left;
}
else {
cur = st.top();
st.pop();
result.push_back(cur->val);
cur = cur->right;
}
}
return result;
}
};
// 后序遍历 中左右->中右左->左右中
class post {
public:
vector<int> postorderTraversal(TreeNode *root) {
stack<TreeNode *> st;
vector<int> result;
if (root == nullptr) return result;
st.push(root);
while (!st.empty()) {
TreeNode *node = st.top();
st.pop();
result.push_back(node->val);
if (node->left)
st.push(node->left); // 相对于前序遍历,这更改一下入栈顺序 (空节点不入栈)
if (node->right) st.push(node->right); // 空节点不入栈
}
reverse(result.begin(), result.end()); // 将结果反转之后就是左右中的顺序了
return result;
}
};
// 二叉树的统一迭代法 使用空指针进行标记
class inorder2 {
public:
vector<int> inorderTraversal(TreeNode *root) {
vector<int> result;
stack<TreeNode *> st;
if (root != nullptr) st.push(root);
while (!st.empty()) {
TreeNode *node = st.top();
if (node != nullptr) {
st.pop(); // 将该节点弹出,避免重复操作,下面再将右中左节点添加到栈中
if (node->right) st.push(node->right); // 添加右节点(空节点不入栈)
st.push(node); // 添加中节点
st.push(nullptr); // 中节点访问过,但是还没有处理,加入空节点做为标记。
if (node->left) st.push(node->left); // 添加左节点(空节点不入栈)
}
else { // 只有遇到空节点的时候,才将下一个节点放进结果集
st.pop(); // 将空节点弹出
node = st.top(); // 重新取出栈中元素
st.pop();
result.push_back(node->val); // 加入到结果集
}
}
return result;
}
};
class lc102 {
public:
vector<vector<int>> levelOrder(TreeNode *root) {
queue<TreeNode *> queue;
vector<vector<int>> result;
vector<int> tmp;
if (root != nullptr) { queue.push(root); }
while (!queue.empty()) {
tmp = vector<int>{};
int size = queue.size();
for (int i = 0; i < size; i++) {
TreeNode *cur = queue.front();
queue.pop();
tmp.push_back(cur->val);
if (cur->left) queue.push(cur->left);
if (cur->right) queue.push(cur->right);
}
result.push_back(tmp);
}
// LC107 reverse(result.begin(), result.end());
return result;
}
};
class lc199 {
public:
vector<int> rightSideView(TreeNode *root) {
queue<TreeNode *> queue;
vector<int> result;
vector<int> tmp;
if (root != nullptr) { queue.push(root); }
while (!queue.empty()) {
int size = queue.size();
tmp = vector<int>{};
for (int i = 0; i < size; i++) {
TreeNode *cur = queue.front();
queue.pop();
tmp.push_back(cur->val);
if (cur->left) queue.push(cur->left);
if (cur->right) queue.push(cur->right);
}
result.push_back(tmp[size - 1]);
}
return result;
}
};
class lc637 {
public:
vector<double> averageOfLevels(TreeNode *root) {
queue<TreeNode *> queue;
vector<double> result;
if (root != nullptr) { queue.push(root); }
while (!queue.empty()) {
int size = queue.size();
vector<int> tmp{};
double sumOfLevels = 0;
for (int i = 0; i < size; i++) {
TreeNode *cur = queue.front();
queue.pop();
tmp.push_back(cur->val);
if (cur->left) queue.push(cur->left);
if (cur->right) queue.push(cur->right);
}
for (auto it = tmp.begin(); it != tmp.end(); it++) { sumOfLevels += *it; }
result.push_back(sumOfLevels / tmp.size());
}
return result;
}
};
class Node {
public:
int val;
vector<Node *> children;
Node() {}
Node(int _val) { val = _val; }
Node(int _val, vector<Node *> _children) {
val = _val;
children = _children;
}
};
class lc429 {
public:
vector<vector<int>> levelOrder(Node *root) {
queue<Node *> queue;
vector<vector<int>> result;
vector<int> tmp;
if (root != nullptr) { queue.push(root); }
while (!queue.empty()) {
tmp = vector<int>{};
int size = queue.size();
for (int i = 0; i < size; i++) {
Node *cur = queue.front();
queue.pop();
tmp.push_back(cur->val);
for (int i = 0; i < cur->children.size(); i++) {
if (cur->children[i]) { queue.push(cur->children[i]); }
}
}
result.push_back(tmp);
}
return result;
}
};
class lc515 {
public:
vector<int> largestValues(TreeNode *root) {
queue<TreeNode *> queue;
vector<int> result;
if (root != nullptr) { queue.push(root); }
while (!queue.empty()) {
int size = queue.size();
vector<int> tmp{};
int maxValue = INT32_MIN;
for (int i = 0; i < size; i++) {
TreeNode *cur = queue.front();
queue.pop();
tmp.push_back(cur->val);
if (cur->left) queue.push(cur->left);
if (cur->right) queue.push(cur->right);
}
for (auto it = tmp.begin(); it != tmp.end(); it++) {
maxValue = (maxValue < *it) ? *it : maxValue;
}
result.push_back(maxValue);
}
return result;
}
};
// 求最大深度按逻辑
class lc104_recursion {
public:
int result;
void getDepth(TreeNode *node, int depth) {
result = depth > result ? depth : result; // 中
if (node->left == NULL && node->right == NULL) return;
if (node->left) { // 左
depth++; // 深度+1
getDepth(node->left, depth);
depth--; // 回溯,深度-1
}
if (node->right) { // 右
depth++; // 深度+1
getDepth(node->right, depth);
depth--; // 回溯,深度-1
}
return;
}
int maxDepth(TreeNode *root) {
result = 0;
if (root == NULL) return result;
getDepth(root, 1);
return result;
}
};
class lc101 {
public:
bool compareNode(TreeNode *left, TreeNode *right) {
if (left == nullptr && right != nullptr)
return false;
else if (left != nullptr && right == nullptr)
return false;
else if (left == nullptr && right == nullptr)
return true;
else if (left->val != right->val)
return false;
return compareNode(left->left, right->right) && compareNode(left->right, right->left);
}
bool isSymmetric(TreeNode *root) {
if (root == nullptr) return true;
return compareNode(root->left, root->right);
}
};
class lc110 {
public:
int balance(TreeNode *node) {
if (node == nullptr) return 0;
int leftTree = balance(node->left);
if (leftTree == -1) return -1;
int rightTree = balance(node->right);
if (rightTree == -1) return -1;
if (abs(leftTree - rightTree) > 1) { return -1; }
else { return max(leftTree, rightTree) + 1; }
}
bool isBalanced(TreeNode *root) {
if (root == nullptr) return true;
return (balance(root) != -1) ? true : false;
}
};
class lc257 {
public:
void traversal(TreeNode *cur, vector<int> &path, vector<string> &result) {
path.push_back(cur->val);
if (cur->left == nullptr && cur->right == nullptr) {
string resultIn;
for (int i = 0; i < path.size() - 1; i++) {
resultIn += to_string(path[i]);
resultIn += "->";
}
resultIn += to_string(path[path.size() - 1]);
result.push_back(resultIn);
}
if (cur->left) {
traversal(cur->left, path, result);
path.pop_back();
}
if (cur->right) {
traversal(cur->right, path, result);
path.pop_back();
}
}
vector<string> binaryTreePaths(TreeNode *root) {
vector<string> result;
vector<int> path;
if (root == NULL) return result;
traversal(root, path, result);
return result;
}
};
class lc404 {
public:
int sumOfLeftLeaves1(TreeNode *root) {
queue<TreeNode *> que;
if (root != nullptr) { que.push(root); }
int result = 0;
while (!que.empty()) {
TreeNode *node = que.front();
que.pop();
if (node->left != nullptr && node->left->left == nullptr &&
node->left->right == nullptr) {
result += node->left->val;
}
if (node->left) que.push(node->left);
if (node->right) que.push(node->right);
}
return result;
}
int sumOfLeftLeaves(TreeNode *root) {
if (root == nullptr) return 0;
int resultLeft = 0;
int resultRight = 0;
resultLeft = sumOfLeftLeaves(root->left);
if (root->left != nullptr && root->left->left == nullptr && root->left->right == nullptr) {
resultLeft = root->left->val;
}
resultRight = sumOfLeftLeaves(root->right);
return resultLeft + resultRight;
}
};
class lc513 {
public:
int maxDepth = INT_MIN;
int result;
void traversal(TreeNode *root, int depth) {
if (root->left == nullptr && root->right == nullptr) {
if (depth > maxDepth) {
maxDepth = depth;
result = root->val;
}
}
if (root->left) {
depth++;
traversal(root->left, depth);
depth--;
}
if (root->right) {
depth++;
traversal(root->right, depth);
depth--;
}
}
int findBottomLeftValue(TreeNode *root) {
traversal(root, 1);
return result;
}
};
class lc112 {
public:
bool traversal(TreeNode *cur, int count) {
if (cur->left == nullptr && cur->right == nullptr && count == 0) return true;
if (cur->left == nullptr && cur->right == nullptr) return false;
if (cur->left) {
if (traversal(cur->left, count - cur->left->val)) return true;
}
if (cur->right) {
if (traversal(cur->right, count - cur->right->val)) return true;
}
return false;
}
bool hasPathSum(TreeNode *root, int targetSum) {
if (root == nullptr) return false;
return traversal(root, targetSum - root->val);
}
};
class lc113 {
public:
void traversal(TreeNode *cur, int count, vector<vector<int>> &result, vector<int> &path) {
path.push_back(cur->val);
if (cur->left == nullptr && cur->right == nullptr && count == 0) { result.push_back(path); }
if (cur->left == nullptr && cur->right == nullptr) return;
if (cur->left) {
traversal(cur->left, count - cur->left->val, result, path);
path.pop_back();
}
if (cur->right) {
traversal(cur->right, count - cur->right->val, result, path);
path.pop_back();
}
}
vector<vector<int>> pathSum(TreeNode *root, int targetSum) {
vector<vector<int>> result;
vector<int> path;
if (root == nullptr) return result;
traversal(root, targetSum - root->val, result, path);
return result;
}
};
class lc106 {
public:
TreeNode *traversal(vector<int> &inorder, vector<int> &postorder) {
if (postorder.size() == 0) return nullptr;
TreeNode *node = new TreeNode(postorder[postorder.size() - 1]);
if (postorder.size() == 1) return node;
int cutPos = 0;
while (true) {
if (inorder[cutPos++] == postorder[postorder.size() - 1]) break;
}
cutPos--;
vector<int> leftIn = vector<int>(inorder.begin(), inorder.begin() + cutPos);
vector<int> rightIn = vector<int>(inorder.begin() + cutPos + 1, inorder.end());
vector<int> leftPost = vector<int>(postorder.begin(), postorder.begin() + leftIn.size());
vector<int> rightPost = vector<int>(postorder.begin() + leftIn.size(), postorder.end() - 1);
node->left = traversal(leftIn, leftPost);
node->right = traversal(rightIn, rightPost);
return node;
}
TreeNode *buildTree(vector<int> &inorder, vector<int> &postorder) {
if (inorder.size() == 0) return nullptr;
return traversal(inorder, postorder);
}
};
class lc105 {
public:
TreeNode *buildTree(vector<int> &preorder, vector<int> &inorder) {
if (preorder.empty()) return nullptr;
TreeNode *root = new TreeNode(preorder[0]);
if (preorder.size() == 1) return root;
int cutPos;
for (cutPos = 0; cutPos < preorder.size(); cutPos++) {
if (inorder[cutPos] == preorder[0]) break;
}
vector<int> leftin = vector<int>(inorder.begin(), inorder.begin() + cutPos);
vector<int> rightin = vector<int>(inorder.begin() + cutPos + 1, inorder.end());
vector<int> leftpre =
vector<int>(preorder.begin() + 1, preorder.begin() + leftin.size() + 1);
vector<int> rightpre = vector<int>(preorder.begin() + leftin.size() + 1, preorder.end());
root->left = buildTree(leftpre, leftin);
root->right = buildTree(rightpre, rightin);
return root;
}
};
class lc654 {
public:
TreeNode *constructMaximumBinaryTree(vector<int> &nums) {
TreeNode *node = new TreeNode(0);
if (nums.size() == 1) {
node->val = nums[0];
return node;
}
pair<int, int> max_value_index(INT32_MIN, -1);
for (int i = 0; i < nums.size(); i++) {
if (nums[i] > max_value_index.first) {
max_value_index.first = nums[i];
max_value_index.second = i;
}
}
node->val = max_value_index.first;
if (max_value_index.second > 0) {
vector<int> leftVec(nums.begin(), nums.begin() + max_value_index.second);
node->left = constructMaximumBinaryTree(leftVec);
}
if (max_value_index.second < nums.size() - 1) {
vector<int> rightVec(nums.begin() + max_value_index.second + 1, nums.end());
node->right = constructMaximumBinaryTree(rightVec);
}
return node;
}
};
class lc617 {
public:
TreeNode *mergeTrees(TreeNode *root1, TreeNode *root2) {
if (root1 == nullptr) return root2;
if (root2 == nullptr) return root1;
// Both trees are non-empty; merge them
// TreeNode *root = new TreeNode(root1->val + root2->val);
// 原地修改效率高
root1->val += root2->val;
root1->left = mergeTrees(root1->left, root2->left);
root1->right = mergeTrees(root1->right, root2->right);
return root1;
}
};
// 二叉搜索树
class lc700 {
public:
TreeNode *searchBST(TreeNode *root, int val) {
if (root == nullptr) return nullptr;
if (root->val == val) return root;
if (root->val > val) return searchBST(root->left, val);
if (root->val < val) return searchBST(root->right, val);
return nullptr;
}
};
// 二叉搜索树的验证
class lc98 {
public:
void sortInorder(TreeNode *root, vector<int> &sortvec) {
if (root == nullptr) return;
sortInorder(root->left, sortvec);
sortvec.push_back(root->val);
sortInorder(root->right, sortvec);
}
bool isValidBST(TreeNode *root) {
vector<int> sortvec;
sortInorder(root, sortvec);
for (int i = 0; i < sortvec.size() - 1; i++) {
if (sortvec[i] >= sortvec[i + 1]) return false;
}
return true;
}
};
// 二叉搜索树的最小绝对差
class lc530 {
public:
int result = INT_MAX;
TreeNode *pre = nullptr;
int getMinimumDifference(TreeNode *root) {
if (root == nullptr) return result;
getMinimumDifference(root->left);
if (pre != nullptr) result = min(result, root->val - pre->val);
pre = root;
getMinimumDifference(root->right);
return result;
}
};
// 二叉搜索树中的众数
class lc501 {
public:
vector<int> result;
TreeNode *pre;
int count;
int maxcount;
lc501() : result(vector<int>()), pre(nullptr), count(0), maxcount(0) {}
vector<int> findMode(TreeNode *root) {
if (!root) return result;
findMode(root->left);
if (pre == nullptr) { count = 1; }
else if (pre->val == root->val) { count++; }
else { count = 1; }
if (count == maxcount) { result.push_back(root->val); }
else if (count > maxcount) {
maxcount = count;
result.clear();
result.push_back(root->val);
}
findMode(root->right);
return result;
}
};
// 二叉树最近公共祖先
class lc236 {
public:
TreeNode *lowestCommonAncestor(TreeNode *root, TreeNode *p, TreeNode *q) {
if (root == nullptr || root == p || root == q) return root;
TreeNode *left = lowestCommonAncestor(root->left, p, q);
TreeNode *right = lowestCommonAncestor(root->right, p, q);
if (left && right) { return root; }
else if (left) { return left; }
else if (right) { return right; }
else { return nullptr; }
}
};
// BST最近公共祖先
class lc235 {
public:
TreeNode *lowestCommonAncestor(TreeNode *root, TreeNode *p, TreeNode *q) {
if (root->val > p->val && root->val > q->val) {
return lowestCommonAncestor(root->left, p, q);
}
else if (root->val < p->val && root->val < q->val) {
return lowestCommonAncestor(root->right, p, q);
}
else
return root;
}
};
// 二叉搜索树的插入
// 记得再看看迭代法
class lc701 {
public:
TreeNode *insertIntoBST(TreeNode *root, int val) {
if (root == nullptr) // 中
{
TreeNode *node = new TreeNode(val);
return node;
}
if (root->val > val) { root->left = insertIntoBST(root->left, val); } // 左
else if (root->val < val) { root->right = insertIntoBST(root->right, val); } // 右
return root;
}
};
// 删除BST的节点
class lc450 {
public:
TreeNode *deleteNode(TreeNode *root, int key) {
if (root == nullptr) return nullptr;
if (root->val == key) {
if (!root->left && !root->right) {
delete root;
return nullptr;
}
else if (!root->left) {
TreeNode *node = root->right;
delete root;
return node;
}
else if (!root->right) {
TreeNode *node = root->left;
delete root;
return node;
}
else {
TreeNode *rightleave = root->right;
while (!rightleave->left) { rightleave = rightleave->left; }
rightleave->left = root->left;
root->left = nullptr;
TreeNode *node2 = root->right;
delete root;
return node2;
}
}
else if (root->val > key)
root->left = deleteNode(root->left, key);
else
root->right = deleteNode(root->right, key);
return root;
}
TreeNode *solution2(TreeNode *root, int key) {
if (root == nullptr) return root;
if (root->val == key) {
if (root->right == nullptr) { // 这里第二次操作目标值:最终删除的作用
return root->left;
}
TreeNode *cur = root->right;
while (cur->left) { cur = cur->left; }
swap(root->val, cur->val); // 这里第一次操作目标值:交换目标值其右子树最左面节点。
}
root->left = deleteNode(root->left, key);
root->right = deleteNode(root->right, key);
return root;
}
};
// 修剪二叉搜索树
class lc669 {
public:
TreeNode *trimBST(TreeNode *root, int low, int high) {
if (!root) return nullptr;
if (root->val < low) return trimBST(root->right, low, high);
if (root->val > high) return trimBST(root->left, low, high);
root->left = trimBST(root->left, low, high);
root->right = trimBST(root->right, low, high);
return root;
}
};
// 将有序数组转换为二叉搜索树
class lc108 {
public:
TreeNode *sortedArrayToBST(vector<int> &nums) {
TreeNode *node = new TreeNode(0);
if (nums.size() == 1) {
node->val = nums[0];
return node;
}
int seg = nums.size() / 2;
vector<int> left(nums.begin(), nums.begin() + seg);
vector<int> right(nums.begin() + seg + 1, nums.end());
TreeNode *root = new TreeNode(nums[seg]);
if (left.size() > 0) root->left = sortedArrayToBST(left);
if (right.size() > 0) root->right = sortedArrayToBST(right);
return root;
}
};
// 二叉树转为累加
class lc538 {
public:
int pre = 0;
void traversal(TreeNode *cur) {
if (cur == nullptr) return;
traversal(cur->right);
cur->val += pre;
pre = cur->val;
traversal(cur->left);
}
TreeNode *convertBST(TreeNode *root) {
traversal(root);
return root;
}
};
int main() {
vector<int> a{3, 9, 20, 15, 7};
vector<int> b{9, 3, 15, 20, 7};
}