@@ -12,56 +12,68 @@ public Node(int data) {
1212}
1313}
1414
15+ // 第一种方法
16+ // 收集整棵树的高度h,和节点数n
17+ // 只有满二叉树满足 : 2 ^ h - 1 == n
1518public static boolean isFull1 (Node head ) {
1619if (head == null ) {
1720return true ;
1821}
19- int height = h (head );
20- int nodes = n (head );
21- return (1 << height ) - 1 == nodes ;
22+ Info1 all = process1 (head );
23+ return (1 << all .height ) - 1 == all .nodes ;
2224}
2325
24- public static int h (Node head ) {
25- if (head == null ) {
26- return 0 ;
26+ public static class Info1 {
27+ public int height ;
28+ public int nodes ;
29+
30+ public Info1 (int h , int n ) {
31+ height = h ;
32+ nodes = n ;
2733}
28- return Math .max (h (head .left ), h (head .right )) + 1 ;
2934}
3035
31- public static int n (Node head ) {
36+ public static Info1 process1 (Node head ) {
3237if (head == null ) {
33- return 0 ;
38+ return new Info1 ( 0 , 0 ) ;
3439}
35- return n (head .left ) + n (head .right ) + 1 ;
40+ Info1 leftInfo = process1 (head .left );
41+ Info1 rightInfo = process1 (head .right );
42+ int height = Math .max (leftInfo .height , rightInfo .height ) + 1 ;
43+ int nodes = leftInfo .nodes + rightInfo .nodes + 1 ;
44+ return new Info1 (height , nodes );
3645}
3746
47+ // 第二种方法
48+ // 收集子树是否是满二叉树
49+ // 收集子树的高度
50+ // 左树满 && 右树满 && 左右树高度一样 -> 整棵树是满的
3851public static boolean isFull2 (Node head ) {
3952if (head == null ) {
4053return true ;
4154}
42- Info all = process (head );
43- return (1 << all .height ) - 1 == all .nodes ;
55+ return process2 (head ).isFull ;
4456}
4557
46- public static class Info {
58+ public static class Info2 {
59+ public boolean isFull ;
4760public int height ;
48- public int nodes ;
4961
50- public Info (int h , int n ) {
62+ public Info2 (boolean f , int h ) {
63+ isFull = f ;
5164height = h ;
52- nodes = n ;
5365}
5466}
5567
56- public static Info process (Node head ) {
57- if (head == null ) {
58- return new Info ( 0 , 0 );
68+ public static Info2 process2 (Node h ) {
69+ if (h == null ) {
70+ return new Info2 ( true , 0 );
5971}
60- Info leftInfo = process (head .left );
61- Info rightInfo = process (head .right );
72+ Info2 leftInfo = process2 (h .left );
73+ Info2 rightInfo = process2 (h .right );
74+ boolean isFull = leftInfo .isFull && rightInfo .isFull && leftInfo .height == rightInfo .height ;
6275int height = Math .max (leftInfo .height , rightInfo .height ) + 1 ;
63- int nodes = leftInfo .nodes + rightInfo .nodes + 1 ;
64- return new Info (height , nodes );
76+ return new Info2 (isFull , height );
6577}
6678
6779// for test
@@ -84,13 +96,14 @@ public static void main(String[] args) {
8496int maxLevel = 5 ;
8597int maxValue = 100 ;
8698int testTimes = 1000000 ;
99+ System .out .println ("测试开始" );
87100for (int i = 0 ; i < testTimes ; i ++) {
88101Node head = generateRandomBST (maxLevel , maxValue );
89102if (isFull1 (head ) != isFull2 (head )) {
90- System .out .println ("Oops !" );
103+ System .out .println ("出错了 !" );
91104}
92105}
93- System .out .println ("finish! " );
106+ System .out .println ("测试结束 " );
94107}
95108
96109}
0 commit comments