-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathCompleteBinaryTree.java
36 lines (33 loc) · 1.1 KB
/
CompleteBinaryTree.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
/*https://practice.geeksforgeeks.org/problems/complete-binary-tree/1*/
class GfG
{
boolean isCompleteBT(Node root)
{
//add code here.
int exp = 1, len, i;
Queue<Node> queue = new LinkedList<Node>();
Node node;
queue.add(root);
boolean broke = false;
boolean lastLevel = false;
while (!queue.isEmpty())
{
broke = false;
len = queue.size();
for (i = 0; i < len; ++i)
{
node = queue.poll();
if (node.left != null && (broke || lastLevel)) return false;
else if (node.left != null) queue.add(node.left);
else if (node.left == null) broke = true;
if (node.right != null && (broke || lastLevel)) return false;
else if (node.right != null) queue.add(node.right);
else if (node.right == null) broke = true;
}
if (broke && queue.size() == 0) return true;
else if (broke) lastLevel = true;
exp <<= 1;
}
return true;
}
}