-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathCheck_958.java
40 lines (37 loc) · 1.17 KB
/
Check_958.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
import javafx.util.Pair;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;
public class Check_958 {
// Most voted solution using BFS
public boolean isCompleteTree(TreeNode root) {
Queue<TreeNode> bfs = new LinkedList<TreeNode>();
bfs.offer(root);
while (bfs.peek() != null) {
TreeNode node = bfs.poll();
bfs.offer(node.left);
bfs.offer(node.right);
}
while (!bfs.isEmpty() && bfs.peek() == null)
bfs.poll();
return bfs.isEmpty();
}
// Standard solution using BFS
/*
public boolean isCompleteTree(TreeNode root) {
List<Pair<TreeNode, Integer>> q = new ArrayList<>();
q.add(new Pair<>(root, 1));
int i = 0;
while(i < q.size()) {
Pair<TreeNode, Integer> tmp = q.get(i++);
TreeNode node = tmp.getKey();
int code = tmp.getValue();
if (node == null) continue;
q.add(new Pair<>(node.left, 2 * code));
q.add(new Pair<>(node.right, 2 * code + 1));
}
return q.get(i - 1).getValue() == q.size();
}
*/
}