File tree 1 file changed +14
-15
lines changed
1 file changed +14
-15
lines changed Original file line number Diff line number Diff line change @@ -19,7 +19,7 @@ var isCompleteTree = function(root) {
19
19
// bfs until n-1 level of depth
20
20
21
21
const getDepth = ( node ) => {
22
- if ( ! node ) return 0 ;
22
+ if ( ! node ) return 0 ;
23
23
return 1 + Math . max ( getDepth ( node . left ) , getDepth ( node . right ) ) ;
24
24
}
25
25
@@ -29,43 +29,43 @@ var isCompleteTree = function(root) {
29
29
q . enqueue ( root ) ;
30
30
31
31
const checkLastLevel = ( arr ) => {
32
- while ( arr [ arr . length - 1 ] === null ) arr . pop ( ) ;
32
+ while ( arr [ arr . length - 1 ] === null ) arr . pop ( ) ;
33
33
34
34
let i = 0 ;
35
- while ( i < arr . length ) {
36
- if ( arr [ i ] === null ) return false ;
35
+ while ( i < arr . length ) {
36
+ if ( arr [ i ] === null ) return false ;
37
37
i ++ ;
38
38
}
39
39
40
40
return true ;
41
41
}
42
42
43
43
let i = 0 ;
44
- while ( i < depth ) {
44
+ while ( i < depth ) {
45
45
46
46
let size = q . size ( ) ;
47
47
48
- if ( size !== 2 ** i ) return false ;
48
+ if ( size !== 2 ** i ) return false ;
49
49
50
- while ( size ) {
50
+ while ( size ) {
51
51
const node = q . dequeue ( ) ;
52
- if ( ! node . left && i !== depth - 1 ) return false ;
53
- if ( ! node . right && i !== depth - 1 ) return false ;
52
+ if ( ! node . left && i !== depth - 1 ) return false ;
53
+ if ( ! node . right && i !== depth - 1 ) return false ;
54
54
55
- if ( i !== depth - 1 ) {
55
+ if ( i !== depth - 1 ) {
56
56
q . enqueue ( node . left ) ;
57
57
q . enqueue ( node . right ) ;
58
58
} else {
59
59
60
- if ( ! node . left ) {
60
+ if ( ! node . left ) {
61
61
q . enqueue ( null ) ;
62
62
} else {
63
63
q . enqueue ( node . left ) ;
64
64
}
65
65
66
- if ( ! node . right ) {
66
+ if ( ! node . right ) {
67
67
q . enqueue ( null ) ;
68
- } else {
68
+ } else {
69
69
q . enqueue ( node . right ) ;
70
70
}
71
71
@@ -76,7 +76,6 @@ var isCompleteTree = function(root) {
76
76
77
77
i ++ ;
78
78
}
79
-
80
-
79
+
81
80
return checkLastLevel ( q . toArray ( ) ) ;
82
81
} ;
You can’t perform that action at this time.
0 commit comments