Check if a given binary tree is completed. A complete binary tree is one in which every level of the binary tree is completely filled except possibly the last level. Furthermore, all nodes are as far left as possible.

Examples

5

/    \

3        8

/   \

1      4

is completed.

5

/    \

3        8

/   \        \

1      4        11

is not completed.

Corner Cases

  • What if the binary tree is null? Return true in this case.

How is the binary tree represented?

We use the level order traversal sequence with a special symbol "#" denoting the null node.

For Example:

The sequence [1, 2, 3, #, #, 4] represents the following binary tree:

1

/   \

2     3

/

4

/**
* public class TreeNode {
* public int key;
* public TreeNode left;
* public TreeNode right;
* public TreeNode(int key) {
* this.key = key;
* }
* }
*/
public class Solution {
public boolean isCompleted(TreeNode root) {
// Write your solution here
if (root==null){
return true;
}
Queue<TreeNode> q = new LinkedList<TreeNode>();
q.offer(root); int isLeaf = 0;
while(!q.isEmpty()){
Queue<TreeNode> nextQ = new LinkedList<TreeNode>();
int size = q.size();
for(int i=0; i<size; i++){
TreeNode curNode = q.poll();
if(curNode.left==null && curNode.right!=null){
return false;
}
if(curNode.left!=null){
if(isLeaf==1){
return false;
}
nextQ.offer(curNode.left);
}
if(curNode.right!=null){
if(isLeaf==1){
return false;
}
nextQ.offer(curNode.right);
}
if(curNode.left==null || curNode.right==null){
isLeaf=1;
}
}
q = nextQ;
}
return true; }
}

最新文章

  1. MYSQL中UNIX时间戳与日期的转换
  2. 1 Ionic和Hybird应用介绍
  3. Python: 拷贝函数签名
  4. 黑客入门之IP地址及常用命令
  5. TCPCopy 应用
  6. java中equals和hashCode方法的解析
  7. MapReduce shuffle阶段详解
  8. 过度拟合(overfitting)
  9. RADIUS and IPv6[frc-3162译文]
  10. linux下Nginx配置文件(nginx.conf)配置设置详解(windows用phpstudy集成)
  11. Nagios学习实践系列
  12. 02_HTML5+CSS详解第二天
  13. MongoDB 高级索引
  14. C++: 模板函数定义与声明分离;
  15. appium-doctor问题
  16. 巩固python基础
  17. vsftp 基于虚拟用户的ftp服务器 如何做配额
  18. Django学习手册 - 正则URL路由配置/路由分发
  19. 2018.10.20 bzoj1068: [SCOI2007]压缩(区间dp)
  20. JVM基础知识与配置

热门文章

  1. 路飞之-后台日志封装-前后端分离的rbac项目演示-全局异常处理封装-封装Response-luffy数据库创建-软件开发模式-User模块用户表-django的配置文件-开启media访问
  2. vue3 门户网站搭建3-pinia
  3. 问题记录_IDEA版本2021.2.3_debug时变量显示不全(只显示线程附近部分)
  4. 兼容ie8的Html+Css+Js
  5. modelsim仿真含Xilinx原语代码块
  6. qt 运行环境配置
  7. NetBeans的一些快捷键
  8. VMware Fusion Pro 13.0.0 最新序列号【转】
  9. Vue项目的打包方式(生成dist文件)
  10. liunx部署flask项目