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 ...
随机推荐
- JavaScript(3)——文档工具
文档工具 LEARN HTML = 教程 HTML REFERENCE = 字典 HTML + CSS + JAVASCRIPT = DYNAMIC HTML 推荐浏览器: Chrome浏览器(有丰 ...
- 移动端1px 边框
伪类+ transform .border_1px:before{ content: ''; position: absolute; top: 0; height: 1px; width: 100%; ...
- windows使用放大镜快速放大屏幕局部
Win10系统自带放大镜有时真的是比较难使用的,但是如果你对他的快捷键有所了解之后就会感觉它其实也没有那么难,用户可以在使用完之后直接按快捷键将其关闭,一起看看吧. Win10系统放大镜快速关闭快捷键 ...
- TensorFlow实战第一课(session、Variable、Placeholder、Activation Function)
莫烦tensorflow教学 1.session会话控制 Tensorflow 中的Session, Session是 Tensorflow 为了控制,和输出文件的执行的语句. 运行session.r ...
- 【VS开发】设备控制台 (DevCon.exe) 命令
设备控制台 (DevCon.exe) 命令 DevCon (DevCon.exe) 是一个命令行工具,可以显示有关运行 Windows 的计算机上设备的详细信息.还可以使用 DevCon 启用.禁用. ...
- 【Linux开发】linux设备驱动归纳总结(十三):1.触摸屏与ADC时钟
linux设备驱动归纳总结(十三):1.触摸屏与ADC时钟 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx ...
- K8s开始
部署前思考:
- JAVA验证
1.一个JAVA类只能有一个主类. 2.main()方法返回值改为int,不能运行 3.变量作用域有限 实例: 4.数值类型在内存中直接存储其本身的值,对于不同的数值类型,内存中会分配相应的大小去存储 ...
- Luogu P3810 【模板】三维偏序(陌上花开)(CDQ分治)
题目 以三维偏序为例来讲一下CDQ分治. CDQ的本质就是把一个序列分成两段,计算左边对右边的贡献,然后分治. 不过一般都是先分治到底再从下往上算,这样可以先归并再算. 比如这道题,我们先按第一维排序 ...
- WINDOWS7 系统中建立文件夹映射
如何在WIN7中建立文件夹映射,还有以及MKLINK的具体使用方法: 步骤如下: 1.以映射d盘1文件夹为例: 2.按win+r,输入cmd,点击确定: 3.提示符后输入mklink /J " ...