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 ...
随机推荐
- 基于excel的自动化框架
设定项目文件大致结构 atp/: 项目名 conf/:存放配置文件 data/:存放sql文件 lib/: 存放项目的所有源代码. logs/:存放日志文件 uploads/:存放下载的文件 star ...
- Python列表等长度分割
1 def list_of_groups(init_list, childern_list_len): 2 ''' 3 :param init_list: 4 :param childern_list ...
- 有道翻译-JS逆向-api调用
简单方法 -调用开源api 这个比较简单四行代码就可以搞定,先放代码: 1 import requests 2 while True: 3 input_data = input('请输入你要翻译的数据 ...
- Tomcat集群配置--负载均衡
Tomcat集群配置学习篇-----分布式应用 现目前基于javaWeb开发的应用系统已经比比皆是,尤其是电子商务网站,要想网站发展壮大,那么必然就得能够承受住庞大的网站访问量:大家知道如果服务器访问 ...
- VUE3 API之watch与watchEffect
watch(source,callback,options) 官方术语:侦听一个或多个响应式数据源,并在数据源变化时调用所给的回调函数. watchEffect(effect,options) 官方术 ...
- Python系统模块os.py和sys.py常用函数
OS模块 os模块就是对操作系统进行操作,使用该模块必须先导入模块: import os #getcwd() 获取当前工作目录(当前工作目录默认都是当前文件所在的文件夹) result = os.ge ...
- SpringBoot为什么这么火?
1. 总的设计原则是""默认大于配置"" 2. Starter机制,开箱即用,默认的配置和依赖都是默认加载的 3. SpringBoot是Spring的子类, ...
- 本地开发环境使用redis
1.使用cmd 连接后,查询结果出现乱码时, 尝试连接时加--raw 参数 2.如果还是乱码,设置cmd 窗口编码 chcp 65001 就是换成UTF-8代码页chcp 936 可以换回默认的GBK ...
- centos7.4系统: redis配置密码
背景:因为安全需要,对redis进行密码配置 说明:默认redis没有密码,需要自己配置密码 一.配置临时密码(重启后失效) 以下以密码:wangzy 为例 1.1 连接客户端 [root@wangz ...
- Respecting causality is all you need for training physics-informed neural networks
未发表 本篇工作时关于连续时间的PDE.也是从因果关系的角度入手,最近看过几篇该作者的工作.(简而言之就是从初始条件方向开始训练) 目前的PINN框架缺乏尊重物理系统演化所固有的时空因果结构.因此,作 ...