Created
June 22, 2017 09:26
-
-
Save Kaushal28/d633e439af0259fc2aac352fee699536 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class GfG{ | |
boolean isLeaf(Node node){ | |
return node.left==null && node.right==null; | |
} | |
boolean isComplete(Node node){ | |
return node.left!=null && node.right!=null; | |
} | |
boolean isEmptyLeftButFullRight(Node node){ | |
return node.left==null && node.right!=null; | |
} | |
boolean isCompleteBT(Node root){ | |
Queue<Node> q = new LinkedList<>(); | |
q.add(root); | |
boolean flag = false; | |
while(q.size()!=0){ | |
Node current = q.poll(); | |
if(flag && !isLeaf(current)) return false; | |
if(!isComplete(current)) flag = true; | |
if(isEmptyLeftButFullRight(current)) return false; | |
if(current.left!=null) q.add(current.left); | |
if(current.right!=null) q.add(current.right); | |
} | |
return true; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment