This action will force synchronization from wangjili/Algorithm, which will overwrite any changes that you have made since you forked the repository, and can not be recovered!!!
Synchronous operation will process in the background and will refresh the page when finishing processing. Please be patient.
package part7tree;import java.util.LinkedList;import java.util.Queue;/*** Created by Dell on 2017年07月27日.*/public class CheckCompletion {public class A{}//检查是否是完全二叉树,只需要层序遍历即可public boolean chk(TreeNode root) {Queue<TreeNode> queue=new LinkedList<TreeNode>();queue.add(root);TreeNode cur=null;boolean flag=false;//标志是否出现节点不全的情况,true为出现,后面的节点必须全为叶节点//检查的过程中检查每个节点子节点的情况,一旦发现一个节点不是两个节点全有,就修改flagwhile (!queue.isEmpty()){cur=queue.poll();if(flag){if(cur.left!=null||cur.right!=null){return false;}}else{if((cur.left==null&&cur.right==null)||(cur.left!=null&&cur.right==null)){//出现为空flag=true;}else if(cur.left!=null&&cur.right!=null){queue.add(cur.left);queue.add(cur.right);}else{return false;}}}return true;}}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。