-
Notifications
You must be signed in to change notification settings - Fork 0
/
Check If Binary Tree Is Completed.java
80 lines (64 loc) · 1.54 KB
/
Check If Binary Tree Is Completed.java
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
/*
Check if a given binary tree is completed.
A complete binary tree is one in which every level of the binary tree is completely filled except possibly the last level.
Furthermore, all nodes are as far left as possible.
What if the binary tree is null? Return true in this case.
1. miss left child but not right child
2. miss right child, but not the end
Example
1
/ \
2 3
/ \ \
4 5 6
is not a complete tree.
Steps:
Queue cur flag
1 false
2 3 1 false
3 4 5 2 false
4 5 3 true
3.left flag = true
return false
time = O(n)
space = O(n) // extra queue
*/
/**
* public class TreeNode {
* public int key;
* public TreeNode left;
* public TreeNode right;
* public TreeNode(int key) {
* this.key = key;
* }
* }
*/
public class Solution {
public boolean isCompleted(TreeNode root) {
// Write your solution here.
if (root == null) {
return true;
}
Queue<TreeNode> queue = new LinkedList<TreeNode>();
queue.offer(root);
boolean flag = false;
while (!queue.isEmpty()) {
TreeNode cur = queue.poll();
if (cur.left == null) {
flag = true;
} else if (flag) {
return false;
} else {
queue.offer(cur.left);
}
if (cur.right == null) {
flag = true;
} else if(flag) {
return false;
} else {
queue.offer(cur.right);
}
}
return true;
}
}