Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Latest commit

 

History

History
History
1560 lines (1318 loc) · 48.5 KB

File metadata and controls

1560 lines (1318 loc) · 48.5 KB
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package Algorithms.tree;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;
import java.util.Stack;
/**
* REFS:
* http://blog.csdn.net/fightforyourdream/article/details/16843303 面试大总结之二:Java搞定面试中的二叉树题目
* http://blog.csdn.net/luckyxiaoqiang/article/details/7518888 轻松搞定面试中的二叉树题目
* http://www.cnblogs.com/Jax/archive/2009/12/28/1633691.html 算法大全(3) 二叉树
*
* 1. 求二叉树中的节点个数: getNodeNumRec(递归),getNodeNum(迭代)
* 2. 求二叉树的深度: getDepthRec(递归),getDepth
* 3. 前序遍历,中序遍历,后序遍历: preorderTraversalRec, preorderTraversal, inorderTraversalRec, postorderTraversalRec
* (https://en.wikipedia.org/wiki/Tree_traversal#Pre-order_2)
* 4.分层遍历二叉树(按层次从上往下,从左往右): levelTraversal, levelTraversalRec(递归解法)
* 5. 将二叉查找树变为有序的双向链表: convertBST2DLLRec, convertBST2DLL
* 6. 求二叉树第K层的节点个数:getNodeNumKthLevelRec, getNodeNumKthLevel
* 7. 求二叉树中叶子节点的个数:getNodeNumLeafRec, getNodeNumLeaf
* 8. 判断两棵二叉树是否相同的树:isSameRec, isSame
* 9. 判断二叉树是不是平衡二叉树:isAVLRec
* 10. 求二叉树的镜像(破坏和不破坏原来的树两种情况):
* mirrorRec, mirrorCopyRec
* mirror, mirrorCopy
* 10.1 判断两个树是否互相镜像:isMirrorRec isMirror
* 11. 求二叉树中两个节点的最低公共祖先节点:
* LAC 求解最小公共祖先, 使用list来存储path.
* LCABstRec 递归求解BST树.
* LCARec 递归算法 .
* 12. 求二叉树中节点的最大距离:getMaxDistanceRec
* 13. 由前序遍历序列和中序遍历序列重建二叉树:rebuildBinaryTreeRec
* 14. 判断二叉树是不是完全二叉树:isCompleteBinaryTree, isCompleteBinaryTreeRec
* 15. 找出二叉树中最长连续子串(即全部往左的连续节点,或是全部往右的连续节点)findLongest
*/
public class TreeDemo {
/*
1
/ \
2 3
/ \ \
4 5 6
*/
public static void main(String[] args) {
TreeNode r1 = new TreeNode(1);
TreeNode r2 = new TreeNode(2);
TreeNode r3 = new TreeNode(3);
TreeNode r4 = new TreeNode(4);
TreeNode r5 = new TreeNode(5);
TreeNode r6 = new TreeNode(6);
/*
10
/ \
6 14
/ \ \
4 8 16
/
0
*/
/*
1
/ \
2 3
/ \ \
4 5 6
*/
// TreeNode r1 = new TreeNode(10);
// TreeNode r2 = new TreeNode(6);
// TreeNode r3 = new TreeNode(14);
// TreeNode r4 = new TreeNode(4);
// TreeNode r5 = new TreeNode(8);
// TreeNode r6 = new TreeNode(16);
TreeNode r7 = new TreeNode(0);
r1.left = r2;
r1.right = r3;
r2.left = r4;
r2.right = r5;
r3.right = r6;
r4.left = r7;
TreeNode t1 = new TreeNode(10);
TreeNode t2 = new TreeNode(6);
TreeNode t3 = new TreeNode(14);
TreeNode t4 = new TreeNode(4);
TreeNode t5 = new TreeNode(8);
TreeNode t6 = new TreeNode(16);
TreeNode t7 = new TreeNode(0);
TreeNode t8 = new TreeNode(0);
TreeNode t9 = new TreeNode(0);
TreeNode t10 = new TreeNode(0);
TreeNode t11 = new TreeNode(0);
t1.left = t2;
t1.right = t3;
t2.left = t4;
t2.right = t5;
t3.left = t6;
t3.right = t7;
t4.left = t8;
//t4.right = t9;
t5.right = t9;
// test distance
// t5.right = t8;
// t8.right = t9;
// t9.right = t10;
// t10.right = t11;
/*
10
/ \
6 14
/ \ \
4 8 16
/
0
*/
// System.out.println(LCABstRec(t1, t2, t4).val);
// System.out.println(LCABstRec(t1, t2, t6).val);
// System.out.println(LCABstRec(t1, t4, t6).val);
// System.out.println(LCABstRec(t1, t4, t7).val);
// System.out.println(LCABstRec(t1, t3, t6).val);
//
// System.out.println(LCA(t1, t2, t4).val);
// System.out.println(LCA(t1, t2, t6).val);
// System.out.println(LCA(t1, t4, t6).val);
// System.out.println(LCA(t1, t4, t7).val);
// System.out.println(LCA(t1, t3, t6).val);
// System.out.println(LCA(t1, t6, t6).val);
//System.out.println(getMaxDistanceRec(t1));
//System.out.println(isSame(r1, t1));
// System.out.println(isAVLRec(r1));
//
// preorderTraversalRec(r1);
// //mirrorRec(r1);
// //TreeNode r1Mirror = mirror(r1);
//
// TreeNode r1MirrorCopy = mirrorCopy(r1);
// System.out.println();
// //preorderTraversalRec(r1Mirror);
// preorderTraversalRec(r1MirrorCopy);
//
// System.out.println();
//
// System.out.println(isMirrorRec(r1, r1MirrorCopy));
// System.out.println(isMirror(r1, r1MirrorCopy));
//System.out.println(getNodeNumKthLevelRec(r1, 5));
//System.out.println(getNodeNumLeaf(r1));
// System.out.println(getNodeNumRec(null));
// System.out.println(getNodeNum(r1));
//System.out.println(getDepthRec(null));
// System.out.println(getDepth(r1));
//
// preorderTraversalRec(r1);
// System.out.println();
// preorderTraversal(r1);
// System.out.println();
// inorderTraversalRec(r1);
//
// System.out.println();
// inorderTraversal(r1);
// postorderTraversalRec(r1);
// System.out.println();
// postorderTraversal(r1);
// System.out.println();
// levelTraversal(r1);
//
// System.out.println();
// levelTraversalRec(r1);
// TreeNode ret = convertBST2DLLRec(r1);
// while (ret != null) {
// System.out.print(ret.val + " ");
// ret = ret.right;
// }
// TreeNode ret2 = convertBST2DLL(r1);
// while (ret2.right != null) {
// ret2 = ret2.right;
// }
//
// while (ret2 != null) {
// System.out.print(ret2.val + " ");
// ret2 = ret2.left;
// }
//
// TreeNode ret = convertBST2DLL(r1);
// while (ret != null) {
// System.out.print(ret.val + " ");
// ret = ret.right;
// }
// System.out.println();
// System.out.println(findLongest(r1));
// System.out.println();
// System.out.println(findLongest2(r1));
// test the rebuildBinaryTreeRec.
//test_rebuildBinaryTreeRec();
System.out.println(isCompleteBinaryTreeRec(t1));
System.out.println(isCompleteBinaryTree(t1));
}
public static void test_rebuildBinaryTreeRec() {
ArrayList<Integer> list1 = new ArrayList<Integer>();
list1.add(1);
list1.add(2);
list1.add(4);
list1.add(5);
list1.add(3);
list1.add(6);
list1.add(7);
list1.add(8);
ArrayList<Integer> list2 = new ArrayList<Integer>();
list2.add(4);
list2.add(2);
list2.add(5);
list2.add(1);
list2.add(3);
list2.add(7);
list2.add(6);
list2.add(8);
TreeNode root = rebuildBinaryTreeRec(list1, list2);
preorderTraversalRec(root);
System.out.println();
postorderTraversalRec(root);
}
private static class TreeNode{
int val;
TreeNode left;
TreeNode right;
public TreeNode(int val){
this.val = val;
left = null;
right = null;
}
}
/*
* null返回0,然后把左右子树的size加上即可。
* */
public static int getNodeNumRec(TreeNode root) {
if (root == null) {
return 0;
}
return getNodeNumRec(root.left) + getNodeNumRec(root.right) + 1;
}
/**
* 求二叉树中的节点个数迭代解法O(n):基本思想同LevelOrderTraversal,
* 即用一个Queue,在Java里面可以用LinkedList来模拟
*/
public static int getNodeNum(TreeNode root) {
if (root == null) {
return 0;
}
Queue<TreeNode> q = new LinkedList<TreeNode>();
q.offer(root);
int cnt = 0;
while (!q.isEmpty()) {
TreeNode node = q.poll();
if (node.left != null) {
q.offer(node.left);
}
if (node.right != null) {
q.offer(node.right);
}
cnt++;
}
return cnt;
}
public static int getDepthRec(TreeNode root) {
if (root == null) {
return -1;
}
return Math.max(getDepthRec(root.left), getDepthRec(root.right)) + 1;
}
/*
* 可以用 level LevelOrderTraversal 来实现,我们用一个dummyNode来分隔不同的层,这样即可计算出实际的depth.
* 1
/ \
2 3
/ \ \
4 5 6
*
* 在队列中如此排列: 1, dummy, 2, 3, dummy, 4, 5, 5, dummy
*
*/
public static int getDepth(TreeNode root) {
if (root == null) {
return 0;
}
TreeNode dummy = new TreeNode(0);
Queue<TreeNode> q = new LinkedList<TreeNode>();
q.offer(root);
q.offer(dummy);
int depth = -1;
while (!q.isEmpty()) {
TreeNode curr = q.poll();
if (curr == dummy) {
depth++;
if (!q.isEmpty()) { // 使用DummyNode来区分不同的层, 如果下一层不是为空,则应该在尾部加DummyNode.
q.offer(dummy);
}
}
if (curr.left != null) {
q.offer(curr.left);
}
if (curr.right != null) {
q.offer(curr.right);
}
}
return depth;
}
/*
* 3. 前序遍历,中序遍历,后序遍历: preorderTraversalRec, preorderTraversal, inorderTraversalRec, postorderTraversalRec
* (https://en.wikipedia.org/wiki/Tree_traversal#Pre-order_2)
* */
public static void preorderTraversalRec(TreeNode root) {
if (root == null) {
return;
}
System.out.print(root.val + " ");
preorderTraversalRec(root.left);
preorderTraversalRec(root.right);
}
/*
* 前序遍历,Iteration 算法. 把根节点存在stack中。
* */
public static void preorderTraversal(TreeNode root) {
if (root == null) {
return;
}
Stack<TreeNode> s = new Stack<TreeNode>();
s.push(root);
while (!s.isEmpty()) {
TreeNode node = s.pop();
System.out.print(node.val + " ");
if (node.right != null) { //
s.push(node.right);
}
// 我们需要先压入右节点,再压入左节点,这样就可以先弹出左节点。
if (node.left != null) {
s.push(node.left);
}
}
}
/*
* 中序遍历
* */
public static void inorderTraversalRec(TreeNode root) {
if (root == null) {
return;
}
inorderTraversalRec(root.left);
System.out.print(root.val + " ");
inorderTraversalRec(root.right);
}
/**
* 中序遍历迭代解法 ,用栈先把根节点的所有左孩子都添加到栈内,
* 然后输出栈顶元素,再处理栈顶元素的右子树
* http://www.youtube.com/watch?v=50v1sJkjxoc
*
* 还有一种方法能不用递归和栈,基于线索二叉树的方法,较麻烦以后补上
* http://www.geeksforgeeks.org/inorder-tree-traversal-without-recursion-and-without-stack/
*/
public static void inorderTraversal(TreeNode root) {
if (root == null) {
return;
}
Stack<TreeNode> s = new Stack<TreeNode>();
TreeNode cur = root;
while(true) {
// 把当前节点的左节点都push到栈中.
while (cur != null) {
s.push(cur);
cur = cur.left;
}
if (s.isEmpty()) {
break;
}
// 因为此时已经没有左孩子了,所以输出栈顶元素
cur = s.pop();
System.out.print(cur.val + " ");
// 准备处理右子树
cur = cur.right;
}
}
// 后序遍历
/*
* 1
/ \
2 3
/ \ \
4 5 6
if put into the stack directly, then it should be:
1, 2, 4, 5, 3, 6 in the stack.
when pop, it should be: 6, 3, 5, 4, 2, 1
if I
* */
public static void postorderTraversalRec(TreeNode root) {
if (root == null) {
return;
}
postorderTraversalRec(root.left);
postorderTraversalRec(root.right);
System.out.print(root.val + " ");
}
/**
* 后序遍历迭代解法
* http://www.youtube.com/watch?v=hv-mJUs5mvU
* http://blog.csdn.net/tang_jin2015/article/details/8545457
* 从左到右的后序 与从右到左的前序的逆序是一样的,所以就简单喽! 哈哈
* 用另外一个栈进行翻转即可喽
*/
public static void postorderTraversal(TreeNode root) {
if (root == null) {
return;
}
Stack<TreeNode> s = new Stack<TreeNode>();
Stack<TreeNode> out = new Stack<TreeNode>();
s.push(root);
while(!s.isEmpty()) {
TreeNode cur = s.pop();
out.push(cur);
if (cur.left != null) {
s.push(cur.left);
}
if (cur.right != null) {
s.push(cur.right);
}
}
while(!out.isEmpty()) {
System.out.print(out.pop().val + " ");
}
}
/*
* 分层遍历二叉树(按层次从上往下,从左往右)迭代
* 其实就是广度优先搜索,使用队列实现。队列初始化,将根节点压入队列。当队列不为空,进行如下操作:弹出一个节点
* ,访问,若左子节点或右子节点不为空,将其压入队列
* */
public static void levelTraversal(TreeNode root) {
if (root == null) {
return;
}
Queue<TreeNode> q = new LinkedList<TreeNode>();
q.offer(root);
while (!q.isEmpty()) {
TreeNode cur = q.poll();
System.out.print(cur.val + " ");
if (cur.left != null) {
q.offer(cur.left);
}
if (cur.right != null) {
q.offer(cur.right);
}
}
}
public static void levelTraversalRec(TreeNode root) {
ArrayList<ArrayList<Integer>> ret = new ArrayList<ArrayList<Integer>>();
levelTraversalVisit(root, 0, ret);
System.out.println(ret);
}
/**
* 分层遍历二叉树(递归)
* 很少有人会用递归去做level traversal
* 基本思想是用一个大的ArrayList,里面包含了每一层的ArrayList。
* 大的ArrayList的size和level有关系
*
* http://discuss.leetcode.com/questions/49/binary-tree-level-order-traversal#answer-container-2543
*/
public static void levelTraversalVisit(TreeNode root, int level, ArrayList<ArrayList<Integer>> ret) {
if (root == null) {
return;
}
// 如果ArrayList的层数不够用, 则新添加一层
// when size = 3, level: 0, 1, 2
if (level >= ret.size()) {
ret.add(new ArrayList<Integer>());
}
// visit 当前节点
ret.get(level).add(root.val);
// 将左子树, 右子树添加到对应的层。
levelTraversalVisit(root.left, level + 1, ret);
levelTraversalVisit(root.right, level + 1, ret);
}
/*
* 题目要求:将二叉查找树转换成排序的双向链表,不能创建新节点,只调整指针。
查找树的结点定义如下:
既然是树,其定义本身就是递归的,自然用递归算法处理就很容易。将根结点的左子树和右子树转换为有序的双向链表,
然后根节点的left指针指向左子树结果的最后一个结点,同时左子树最后一个结点的right指针指向根节点;
根节点的right指针指向右子树结果的第一个结点,
同时右子树第一个结点的left指针指向根节点。
* */
public static TreeNode convertBST2DLLRec(TreeNode root) {
return convertBST2DLLRecHelp(root)[0];
}
/*
* ret[0] 代表左指针
* ret[1] 代表右指针
* */
public static TreeNode[] convertBST2DLLRecHelp(TreeNode root) {
TreeNode[] ret = new TreeNode[2];
ret[0] = null;
ret[1] = null;
if (root == null) {
return ret;
}
if (root.left != null) {
TreeNode left[] = convertBST2DLLRecHelp(root.left);
left[1].right = root; // 将左子树的尾节点连接到根
root.left = left[1];
ret[0] = left[0];
} else {
ret[0] = root; // 左节点返回root.
}
if (root.right != null) {
TreeNode right[] = convertBST2DLLRecHelp(root.right);
right[0].left = root; // 将右子树的头节点连接到根
root.right = right[0];
ret[1] = right[1];
} else {
ret[1] = root; // 右节点返回root.
}
return ret;
}
/**
* 将二叉查找树变为有序的双向链表 迭代解法
* 类似inOrder traversal的做法
*/
public static TreeNode convertBST2DLL(TreeNode root) {
while (root == null) {
return null;
}
TreeNode pre = null;
Stack<TreeNode> s = new Stack<TreeNode>();
TreeNode cur = root;
TreeNode head = null; // 链表头
while (true) {
while (cur != null) {
s.push(cur);
cur = cur.left;
}
// if stack is empty, just break;
if (s.isEmpty()) {
break;
}
cur = s.pop();
if (head == null) {
head = cur;
}
// link pre and cur.
cur.left = pre;
if (pre != null) {
pre.right = cur;
}
// 左节点已经处理完了,处理右节点
cur = cur.right;
pre = cur;
}
return root;
}
/*
* * 6. 求二叉树第K层的节点个数:getNodeNumKthLevelRec, getNodeNumKthLevel
* */
public static int getNodeNumKthLevel(TreeNode root, int k) {
if (root == null || k <= 0) {
return 0;
}
int level = 0;
Queue<TreeNode> q = new LinkedList<TreeNode>();
q.offer(root);
TreeNode dummy = new TreeNode(0);
int cnt = 0; // record the size of the level.
q.offer(dummy);
while (!q.isEmpty()) {
TreeNode node = q.poll();
if (node == dummy) {
level++;
if (level == k) {
return cnt;
}
cnt = 0; // reset the cnt;
if (q.isEmpty()) {
break;
}
q.offer(dummy);
continue;
}
cnt++;
if (node.left != null) {
q.offer(node.left);
}
if (node.right != null) {
q.offer(node.right);
}
}
return 0;
}
/*
* * 6. 求二叉树第K层的节点个数:getNodeNumKthLevelRec, getNodeNumKthLevel
* */
public static int getNodeNumKthLevelRec(TreeNode root, int k) {
if (root == null || k <= 0) {
return 0;
}
if (k == 1) {
return 1;
}
// 将左子树及右子树在K层的节点个数相加.
return getNodeNumKthLevelRec(root.left, k - 1) + getNodeNumKthLevelRec(root.right, k - 1);
}
/*
* 7. getNodeNumLeafRec 把左子树和右子树的叶子节点加在一起即可
* */
public static int getNodeNumLeafRec(TreeNode root) {
if (root == null) {
return 0;
}
if (root.left == null && root.right == null) {
return 1;
}
return getNodeNumLeafRec(root.left) + getNodeNumLeafRec(root.right);
}
/* 7. getNodeNumLeaf
* 随便使用一种遍历方法都可以,比如,中序遍历。
* inorderTraversal,判断是不是叶子节点。
* */
public static int getNodeNumLeaf(TreeNode root) {
if (root == null) {
return 0;
}
int cnt = 0;
// we can use inorderTraversal travesal to do it.
Stack<TreeNode> s = new Stack<TreeNode>();
TreeNode cur = root;
while (true) {
while (cur != null) {
s.push(cur);
cur = cur.left;
}
if (s.isEmpty()) {
break;
}
// all the left child has been put into the stack, let's deal with the
// current node.
cur = s.pop();
if (cur.left == null && cur.right == null) {
cnt++;
}
cur = cur.right;
}
return cnt;
}
/*
* 8. 判断两棵二叉树是否相同的树。
* 递归解法:
* (1)如果两棵二叉树都为空,返回真
* (2)如果两棵二叉树一棵为空,另一棵不为空,返回假
* (3)如果两棵二叉树都不为空,如果对应的左子树和右子树都同构返回真,其他返回假
* */
public static boolean isSameRec(TreeNode r1, TreeNode r2) {
// both are null.
if (r1 == null && r2 == null) {
return true;
}
// one is null.
if (r1 == null || r2 == null) {
return false;
}
// 1. the value of the root should be the same;
// 2. the left tree should be the same.
// 3. the right tree should be the same.
return r1.val == r2.val &&
isSameRec(r1.left, r2.left) && isSameRec(r1.right, r2.right);
}
/*
* 8. 判断两棵二叉树是否相同的树。
* 迭代解法
* 我们直接用中序遍历来比较就好啦
* */
public static boolean isSame(TreeNode r1, TreeNode r2) {
// both are null.
if (r1 == null && r2 == null) {
return true;
}
// one is null.
if (r1 == null || r2 == null) {
return false;
}
Stack<TreeNode> s1 = new Stack<TreeNode>();
Stack<TreeNode> s2 = new Stack<TreeNode>();
TreeNode cur1 = r1;
TreeNode cur2 = r2;
while (true) {
while (cur1 != null && cur2 != null) {
s1.push(cur1);
s2.push(cur2);
cur1 = cur1.left;
cur2 = cur2.left;
}
if (cur1 != null || cur2 != null) {
return false;
}
if (s1.isEmpty() && s2.isEmpty()) {
break;
}
cur1 = s1.pop();
cur2 = s2.pop();
if (cur1.val != cur2.val) {
return false;
}
cur1 = cur1.right;
cur2 = cur2.right;
}
return true;
}
/*
*
* 9. 判断二叉树是不是平衡二叉树:isAVLRec
* 1. 左子树,右子树的高度差不能超过1
* 2. 左子树,右子树都是平衡二叉树。
*
*/
public static boolean isAVLRec(TreeNode root) {
if (root == null) {
return true;
}
// 左子树,右子树都必须是平衡二叉树。
if (!isAVLRec(root.left) || !isAVLRec(root.right)) {
return false;
}
int dif = Math.abs(getDepthRec(root.left) - getDepthRec(root.right));
if (dif > 1) {
return false;
}
return true;
}
/**
* 10. 求二叉树的镜像 递归解法:
*
* (1) 破坏原来的树
*
* 1 1
* / \
* 2 -----> 2
* \ /
* 3 3
* */
public static TreeNode mirrorRec(TreeNode root) {
if (root == null) {
return null;
}
// 先把左右子树分别镜像,并且交换它们
TreeNode tmp = root.right;
root.right = mirrorRec(root.left);
root.left = mirrorRec(tmp);
return root;
}
/**
* 10. 求二叉树的镜像 Iterator解法:
*
* (1) 破坏原来的树
*
* 1 1
* / \
* 2 -----> 2
* \ /
* 3 3
*
* 应该可以使用任何一种Traversal 方法。
* 我们现在可以试看看使用最简单的前序遍历。
* */
public static TreeNode mirror(TreeNode root) {
if (root == null) {
return null;
}
Stack<TreeNode> s = new Stack<TreeNode>();
s.push(root);
while (!s.isEmpty()) {
TreeNode cur = s.pop();
// 交换当前节点的左右节点
TreeNode tmp = cur.left;
cur.left = cur.right;
cur.right = tmp;
// traversal 左节点,右节点。
if (cur.right != null) {
s.push(cur.right);
}
if (cur.left != null) {
s.push(cur.left);
}
}
return root;
}
/**
* 10. 求二叉树的镜像 Iterator解法:
*
* (2) 创建一个新的树
*
* 1 1
* / \
* 2 -----> 2
* \ /
* 3 3
*
* 应该可以使用任何一种Traversal 方法。
* 我们现在可以试看看使用最简单的前序遍历。
* 前序遍历我们可以立刻把新建好的左右节点创建出来,比较方便
* */
public static TreeNode mirrorCopy(TreeNode root) {
if (root == null) {
return null;
}
Stack<TreeNode> s = new Stack<TreeNode>();
Stack<TreeNode> sCopy = new Stack<TreeNode>();
s.push(root);
TreeNode rootCopy = new TreeNode(root.val);
sCopy.push(rootCopy);
while (!s.isEmpty()) {
TreeNode cur = s.pop();
TreeNode curCopy = sCopy.pop();
// traversal 左节点,右节点。
if (cur.right != null) {
// copy 在这里做比较好,因为我们可以容易地找到它的父节点
TreeNode leftCopy = new TreeNode(cur.right.val);
curCopy.left = leftCopy;
s.push(cur.right);
sCopy.push(curCopy.left);
}
if (cur.left != null) {
// copy 在这里做比较好,因为我们可以容易地找到它的父节点
TreeNode rightCopy = new TreeNode(cur.left.val);
curCopy.right = rightCopy;
s.push(cur.left);
sCopy.push(curCopy.right);
}
}
return rootCopy;
}
/**
* 10. 求二叉树的镜像 递归解法:
*
* (1) 不破坏原来的树,新建一个树
*
* 1 1
* / \
* 2 -----> 2
* \ /
* 3 3
* */
public static TreeNode mirrorCopyRec(TreeNode root) {
if (root == null) {
return null;
Morty Proxy This is a proxified and sanitized view of the page, visit original site.