958. Check Completeness of a Binary Tree
- 题目来源
- C++代码实现
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution
{
public:
bool isCompleteTree(TreeNode* root)
{
if(root == NULL)
{
return true;
}
return Sequencetraversal(root);
}
private:
bool Sequencetraversal(TreeNode* root)
{
queue<TreeNode*> dataqueue;
TreeNode* l = NULL;
TreeNode* r = NULL;
bool leaf = false;
dataqueue.push(root);
while(!dataqueue.empty())
{
root = dataqueue.front();
dataqueue.pop();
l = root->left;
r = root->right;
/*一个节点有右孩子没有左孩子,则必不是完全二叉树*/
if(root->right != NULL && root->left == NULL)
{
return false;
}
// leaf = true 则表示开启了叶节点的判断
//
if(leaf && (r != NULL || l != NULL) )
{
return false;
}
if(l != NULL)
{
dataqueue.push(root->left);
}
if(r != NULL)
{
dataqueue.push(root->right);
}
else
{
leaf = true; //有右孩子没有左孩子 开启叶节点的判断
}
}
return true;
}
};
958. Check Completeness of a Binary Tree的更多相关文章
- leetcode 958. Check Completeness of a Binary Tree 判断是否是完全二叉树 、222. Count Complete Tree Nodes
完全二叉树的定义:若设二叉树的深度为h,除第 h 层外,其它各层 (1-h-1) 的结点数都达到最大个数,第 h 层所有的结点都连续集中在最左边,这就是完全二叉树. 解题思路:将树按照层进行遍历,如果 ...
- LeetCode 958. Check Completeness of a Binary Tree
原题链接在这里:https://leetcode.com/problems/check-completeness-of-a-binary-tree/ 题目: Given a binary tree, ...
- 【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 ...
- 【LeetCode】958. Check Completeness of a Binary Tree 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 BFS DFS 日期 题目地址:https://le ...
- [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 ...
- 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 ...
- Leetcode958. Check Completeness of a Binary Tree二叉树的完全验证性
给定一个二叉树,确定它是否是一个完全二叉树. 百度百科中对完全二叉树的定义如下: 若设二叉树的深度为 h,除第 h 层外,其它各层 (1-h-1) 的结点数都达到最大个数,第 h 层所有的结点都连续集 ...
- 图论-完全二叉树判定-Check Completeness of a Binary Tree
2020-02-19 13:34:28 问题描述: 问题求解: 判定方式就是采用层序遍历,对于一个完全二叉树来说,访问每个非空节点之前都不能访问过null. public boolean isComp ...
- 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 ...
随机推荐
- (2)网络基础之IP
IP分为IPV4和IPV6. 以下只讲IPV4,IPV6后期会重新分出来 (以下均为个人理解,如果有误,欢迎提出.也希望如果转载,能通知我并注明转载信息,毕竟字也是我一个个码出来的.谢谢) IPV4地 ...
- acrobat xi pro 11 补丁激活
acrobat xi pro 11 是一款专门为Adobe Acrobat XI Pro 11制作的破解补丁,这款补丁可以免去软件的激活步骤,让用户可以永久免费使用这款软件.11是款功能强大的pdf设 ...
- vscode配置PHP Debug
1.先在vscode中安装PHP Debug,在设置添加“php.validate.executablePath”项,选中对应版本的php.exe. "php.validate.execut ...
- sqlalchemy一对多的关系
#encoding: utf-8 from sqlalchemy import create_engine,Column,Integer,String,Float,func,and_,or_,Text ...
- CentOS6、7升级Openssh至7.9
出于安全考虑,定期使用Nessus对服务器进行扫描,最新Nessus提示服务器的SSH版本有漏洞,所以把SSH升级到最新版本 1.为了防止升级失败登陆不了,所以需要安装telnet mkdir /ro ...
- java 寻找水仙花数
题目:打印出所有的"水仙花数",所谓"水仙花数"是指一个三位数,其各位数字立方和等于该数本身.例如:153是一个"水仙花数",因为153=1 ...
- 使用Themleaf 模板引擎手动生成html文件
1.为什么要写这一篇呢? 在做一个邮件发送功能的时候,需要发送html邮件,javaMail 发送html 的时候需要有已经生成的html正文,所以需要提前将要发送的内容生成,所以就需要模板引擎来动态 ...
- win10 远程桌面ubuntu16
一. 软件安装 1.1. 打开终端,安装xrdp,vncserver sudo apt-get install xrdp vnc4server xbase-clients 1.2. desktop s ...
- 通过Playbook部署LAMP
Ansible的PlayBook文件格式为YAML语言,所以希望你在编写PlayBook前对YAML语法有一定的了解,否则在运行PlayBook的时候经常碰到语法错误提示,这里我们通过介绍批量部署LA ...
- Python应用RabbitMQ教程
介绍 RabbitMQ是一个消息代理.它的工作就是接收和转发消息.你可以把它想像成一个邮局:你把信件放入邮箱,邮递员就会把信件投递到你的收件人处.在这个比喻中,RabbitMQ就扮演着邮箱.邮局以及邮 ...