Leetcode958. Check Completeness of a Binary Tree二叉树的完全验证性
给定一个二叉树,确定它是否是一个完全二叉树。
百度百科中对完全二叉树的定义如下:
若设二叉树的深度为 h,除第 h 层外,其它各层 (1~h-1) 的结点数都达到最大个数,第 h 层所有的结点都连续集中在最左边,这就是完全二叉树。(注:第 h 层可能包含 1~ 2h 个节点。)
示例 1:

输入:[1,2,3,4,5,6] 输出:true 解释:最后一层前的每一层都是满的(即,结点值为 {1} 和 {2,3} 的两层),且最后一层中的所有结点({4,5,6})都尽可能地向左。
示例 2:

输入:[1,2,3,4,5,null,7] 输出:false 解释:值为 7 的结点没有尽可能靠向左侧。
提示:
- 树中将会有 1 到 100 个结点。
个人递归方法:
class Solution {
bool isTheEnd = false;
int depth = 0;
public:
bool isCompleteTree(TreeNode* root)
{
if (root == NULL)
return true;
depth = GetDepth(root);
return GetAns(root, 1);
}
int GetDepth(TreeNode *root)
{
if (root == NULL)
return 0;
return max(GetDepth(root->left), GetDepth(root->right)) + 1;
}
bool GetAns(TreeNode *root, int dep)
{
//只有根节点的情况,不用判空,因为不会递归到那
if (dep == depth)
{
return true;
}
if (dep < depth - 1)
{
if (root->left == NULL || root->right == NULL)
return false;
return GetAns(root->left, dep + 1) && GetAns(root->right, dep + 1);
}
else if (dep == depth - 1)
{
if (isTheEnd)
{
if (root->left != NULL || root->right != NULL)
return false;
return true;
}
else
{
if (root->left == NULL)
{
if (root->right != NULL)
return false;
isTheEnd = true;
return true;
}
if (root->right == NULL)
{
isTheEnd = true;
return true;
}
return true;
}
}
}
};
广度优先遍历法(推荐):
bool isCompleteTree(TreeNode* root) {
queue<TreeNode *> que;
que.push(root);
while(!que.empty())
{
TreeNode * node = que.front();
que.pop();
if(!node)
{
break;
}
else
{
que.push(node->left);
que.push(node->right);
}
}
while(!que.empty())
{
TreeNode * node=que.front();
if(node)
return false;
que.pop();
}
return true;
}
Leetcode958. Check Completeness of a Binary Tree二叉树的完全验证性的更多相关文章
- [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 层所有的结点都连续集中在最左边,这就是完全二叉树. 解题思路:将树按照层进行遍历,如果 ...
- 【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 ...
- 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 ...
- 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 ...
- 图论-完全二叉树判定-Check Completeness of a Binary Tree
2020-02-19 13:34:28 问题描述: 问题求解: 判定方式就是采用层序遍历,对于一个完全二叉树来说,访问每个非空节点之前都不能访问过null. public boolean isComp ...
- Leetcode 110 Balanced Binary Tree 二叉树
判断一棵树是否是平衡树,即左右子树的深度相差不超过1. 我们可以回顾下depth函数其实是Leetcode 104 Maximum Depth of Binary Tree 二叉树 /** * Def ...
随机推荐
- windows2012 日志查看过程
Windows2012界面修改好造成有些人不知道在哪里查找windows 日志 我这边截图描述一下 1. 2.输入 命令 eventvwr.msc 3.弹出 windows 事件查看器 4.若需要 ...
- bc 进制间转换
我们通过bc的ibase和obase方法来进行进制转换 ibase是输入数字的进制,而obase就是输出数字的进制 两种方式进行转化 交互式的 ==注意:需要先设置obase== [root@dev ...
- leetcode-5-最长回文子串*马拉车
方法一:动态规划 O(n2) O(n2) class Solution: def longestPalindrome(self, s: str) -> str: size = len(s) if ...
- AYITOJ-括号序列-栈的入门
题目描述 给定一个由括号组成的字符串 问其是否为一个合法的括号序列 合法的括号序列的定义如下 1. 空字符串是合法的括号序列 2. 若字符串A是合法的括号序列, 那么{A},[A],(A)也是合法的括 ...
- 初识OpenCV-Python - 007: 平滑图像
本节内容主要将如何平滑图像.如通过低通道滤波模糊图像.或者自定义滤波处理图像. import cv2import numpy as npfrom matplotlib import pyplot as ...
- <数据可视化>Matplotlib(2D+3D)
1.Matplotlib介绍(2D) Matplotlib 是 Python 2D-绘图领域使用最广泛的套件.它能让使用者很轻松地将数据图形化,并且提供多样化的输出格式. pylab 是 matplo ...
- wpf问题集锦
一.今天用vs2013新建wpf程序,项目名称命名为MainWindow,一启动就出现错误:类型“MainWindow.MainWindow”中不存在类型名称"App".博主顿时很 ...
- 一个mdl相关的问题
感觉挺有意义的,细节问题 http://www.kernelmode.info/forum/viewtopic.php?f=14&t=4318这个人代码到底犯了什么错误 I want to p ...
- Nodejs之路(三)—— Nodejs之Express框架
Express 原生的 http 在某些方面表现不足以应对我们的开发需求,所以我们需要使用框架来加快我们的开发效率.框架的目的就是提高效率,让我们的代码更高度统一 在Node 中,有很多 Web 开发 ...
- Java多线程设计模式系列
通过几天的认真阅读,发现这是一本难得一见的好书,为了加深巩固学习成功,我打算将书中的例子全部自己实现一遍,特此记录下来也方便其他朋友学习. 第一章,java语言的线程 单线程程序:打印10000次go ...