刷题之二叉树----判断一棵树是否为完全二叉树
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>
好的,上代码:
<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>
相关文章推荐
- tomcat .do发布是虚拟目录无法访问
- Java设计模式(1)单例模式
- 数据处理---Spring Batch之实践
- Spring事务失效的原因
- 原生下拉刷新SwipeRefreshLayout的使用
- Ionic JPush极光推送 插件实例
- WCF推送
- Jquery遍历数组之$.inArray()方法介绍
- 如何判断程序是否重复运行的几种方法
- C++ 根据扩展名获取文件图标、类型
- MFC改变编辑框背景、字体、颜色等
- Node.js: managing child processes
- 深入理解JVM内幕
- Volley源码解析(三)——图片加载
- c# datagridview列头添加复选框实现全选
- CSS样式覆盖规则
- C++根据扩展名获取文件图标、类型
- 对称加密的对称密钥分发
- VIM配置
- 排序算法(4)——冒泡排序