思路:
好的,上代码:
<span style="font-size:18px;">class CheckCompletion {
public boolean chk(TreeNode root) {
// write code here
if(root == null)
return true;
Queue<TreeNode> q = new LinkedList<TreeNode>();
q.offer(root);
boolean flag = true;
while(!q.isEmpty()){
TreeNode cur = q.poll();
if((cur.left == null &&cur.right !=null) ||
(!flag && (cur.left!=null || cur.right!=null))){
return false;
}
if(cur.left != null)
q.offer(cur.left);
else
flag = false;
if(cur.right != null)
q.offer(cur.right);
else
flag = false;
}
return true;
}
}</span>