您的位置:首页 > 其它

刷题之二叉树----判断一棵树是否为完全二叉树

2016-07-04 17:31 465 查看
思路:



好的,上代码:

<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>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: