Check If Binary Tree Is Completed
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; }
}
Check If Binary Tree Is Completed的更多相关文章
- [leetcode] 94. Binary Tree Inorder Traversal 二叉树的中序遍历
题目大意 https://leetcode.com/problems/binary-tree-inorder-traversal/description/ 94. Binary Tree Inorde ...
- Check whether a given Binary Tree is Complete or not 解答
Question A complete binary tree is a binary tree in which every level, except possibly the last, is ...
- [Swift]LeetCode958. 二叉树的完全性检验 | Check Completeness of a Binary Tree
Given a binary tree, determine if it is a complete binary tree. Definition of a complete binary tree ...
- leetcode 958. Check Completeness of a Binary Tree 判断是否是完全二叉树 、222. Count Complete Tree Nodes
完全二叉树的定义:若设二叉树的深度为h,除第 h 层外,其它各层 (1-h-1) 的结点数都达到最大个数,第 h 层所有的结点都连续集中在最左边,这就是完全二叉树. 解题思路:将树按照层进行遍历,如果 ...
- [Algorithm] Check if a binary tree is binary search tree or not
What is Binary Search Tree (BST) A binary tree in which for each node, value of all the nodes in lef ...
- 115th LeetCode Weekly Contest Check Completeness of a Binary Tree
Given a binary tree, determine if it is a complete binary tree. Definition of a complete binary tree ...
- Coursera Algorithms week4 基础标签表 练习测验:Check if a binary tree is a BST
题目原文: Given a binary tree where each
- LeetCode 958. Check Completeness of a Binary Tree
原题链接在这里:https://leetcode.com/problems/check-completeness-of-a-binary-tree/ 题目: Given a binary tree, ...
- 958. Check Completeness of a Binary Tree
题目来源 题目来源 C++代码实现 /** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode ...
- 【leetcode】958. Check Completeness of a Binary Tree
题目如下: Given a binary tree, determine if it is a complete binary tree. Definition of a complete binar ...
随机推荐
- Web框架-inoic
ionic 下载ionic 使用命令行安装npm 1.安装node.js 最后安装的目录在D:\2019-10-14\2019-11-04-2 检测nodejs安装成功? node -v 如果报错,是 ...
- unittest框架-测试报告模板【BeautifulReport】安装、配置使用、生成带截图的测试报告
一.下载BeautifulReport模块 1.下载BeautifulReport模块 下载地址:https://github.com/TesterlifeRaymond/BeautifulRepor ...
- vue 简单原理
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- 1、app自动化的底层逻辑,adb及monkey和稳定性测试
app自动化的过程中,底层逻辑是计算机通过adb与移动设备进行沟通,告诉移动设备,进行什么操作: 一.概念: Andriod调试桥(adb),是一种命令行工具,可以让我们与设备进行通讯. 二.adb安 ...
- 浅析Winform的可视样式
每一个C#的Winform项目的Main方法里,都有这么一行代码,那么它究竟是用来做什么的呢? Application.EnableVisualStyles(); 从注释来看,这是一行用作设置样式的代 ...
- uniapp开发的app打开微信小程序
第一种 <script> export default { data() { return { sweixin: null } }, onLoad() { this.getPlus() } ...
- Python占位符总结:%方式和format方式
Python中,我们在预定义某类具有相似格式的变量或者输出一句含有多个变量的提示语句时,往往用到占位符,而占位符有两种表达方式: %方式: 下面这段代码摘自matplotlib的_init_.py文件 ...
- 没解决的问题-git连接失败
[parallels@localhost ~]$ ssh-keygen -t rsa -C '1012144290@qq.com'Generating public/private rsa key p ...
- JAVA课程设计(附源码)
Java课程设计选题 Java课程设计说明 本次课程设计的目的是通过课程设计的各个项目的综合训练,培养学生实际分析问题.编程和动手能力,提高学生的综合素质.本课程设计尝试使用一些较生动的设计项目,激发 ...
- 如何基于ZK实现高可用架构
zookeeper设计步骤 设计path 节点的路径 选择znode类型 普通节点.临时节点等 设计znode数据 节点中的数据 设计watch 节点的监听事件以及对应的处理 ZK实现主备切换架构 ...