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 ...
随机推荐
- Using NodeLists
Understanding a NodeList object and its relatives, NamedNodeMap and HTMLCollection, is critical to a ...
- 基于OpenCV的循环行、列移动函数circShift()
///*12 在Matlab中有个circShift()函数,可以实现行.列的循环移动 /// 在返卷积运算中,会用到这个函数.所以,在Opencv中我也定义同样 /// 功能的函数 /// 该函数有 ...
- 关于JavaScript实例化的理解
要理解这个,我们首先要理解一个概念“类”,所谓类,指的是对象的模版.对象就是类的实例.由前面我们知道,对象是单个实物的抽象,所以通常需要一个模版,表示某一类实物的共同特征,然后对象根据这个模版生成,这 ...
- pthon基础知识(索引、切片、序列相加、乘法、检查元素是否是序列成员、计算序列长度、最大最小值)
序列 数据存储方式 数据结构 python 列表.元组.字典.集合.字符串 序列: 一块用于存放多个值的连续内存空间,并且按一定顺序排列,可以通过索引取值 索引(编号): 索引可以是负数 从左到 ...
- 使用 Vulkan * API 并行渲染对象
Vulkan API 是业界最热门的新技术之一.它们支持多线程编程,可以简化跨平台开发,而且主要的芯片.GPU 和设备制造商都为其提供支持.Vulkan API 有望成为未来主流图形渲染平台之一.该平 ...
- 【DSP开发】HyperLink 编程和性能考量
冯华亮/Brighton Feng---Communication Infrastructure 摘要 HyperLink 为两个 KeyStone 架构 DSP 之间提供了一种高速,低延迟,引脚数量 ...
- luoguP1886 滑动窗口(单调队列模板题)
题目链接:https://www.luogu.org/problem/P1886#submit 题意:给定n个数,求大小为k的滑动窗口中最小值和最大值. 思路:单调队列模板题. AC代码: #incl ...
- java 利用辗除法求两个整数的最大公约数和最小公倍数
题目:输入两个正整数m和n,求其最大公约数和最小公倍数. 程序分析:利用辗除法. package Studytest; import java.util.Scanner; public class P ...
- 【转贴】使用sar进行性能分析
使用sar进行性能分析 https://www.cnblogs.com/bangerlee/articles/2545747.html 很早之前就看过 但是自己一直没用过.. 2012-06-12 0 ...
- (4.16)mysql备份还原——物理备份之XtraBackup实践
关键词:XtraBackup实践,物理备份,xtrabackup备份,innobackupex备份 [1]如何使用? [3]系列:innobackupex --help |more [4]系列:xtr ...