Import java.util.LinkedList;
Import Java.util.Queue;
public class Judgetree {
public static Boolean judge (TreeNode root) {
if (root = = null)
return true;
queue<treenode> qu = new linkedlist<treenode> ();
Qu.add (root);
TreeNode temp = null;
int flag = 0;
while (!qu.isempty ()) {
temp = Qu.peek ();
Qu.remove ();
if (temp.left! = NULL && flag = = 0)
Qu.add (Temp.left);
else if (temp.left! = null)
return false;
Else
flag = 1;
if (temp.right! = NULL && flag = = 0)
Qu.add (Temp.right);
else if (temp.right! = null)
return false;
Else
flag = 1;
}
return true;
}
}
Note: Search the solution on the Internet. The sequence traversal, sets the flag bit flag, encounters the null value to set the flag to 1,flag not 1 and the child node of the node is not null, the queue, otherwise (flag 1 and the child node of the node is not NULL, that is, the front is a null junction, Must not be completely binary tree) returns false, neither satisfies the flag of 1 (indicates that a null node was encountered)
Determine if a binary tree is a complete binary tree