给定一个二叉树,确定它是否是一个完全二叉树。

百度百科中对完全二叉树的定义如下:

若设二叉树的深度为 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. 树中将会有 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二叉树的完全验证性的更多相关文章

  1. [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 ...

  2. leetcode 958. Check Completeness of a Binary Tree 判断是否是完全二叉树 、222. Count Complete Tree Nodes

    完全二叉树的定义:若设二叉树的深度为h,除第 h 层外,其它各层 (1-h-1) 的结点数都达到最大个数,第 h 层所有的结点都连续集中在最左边,这就是完全二叉树. 解题思路:将树按照层进行遍历,如果 ...

  3. 【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 ...

  4. 【LeetCode】958. Check Completeness of a Binary Tree 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 BFS DFS 日期 题目地址:https://le ...

  5. 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 ...

  6. LeetCode 958. Check Completeness of a Binary Tree

    原题链接在这里:https://leetcode.com/problems/check-completeness-of-a-binary-tree/ 题目: Given a binary tree, ...

  7. 958. Check Completeness of a Binary Tree

    题目来源 题目来源 C++代码实现 /** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode ...

  8. 图论-完全二叉树判定-Check Completeness of a Binary Tree

    2020-02-19 13:34:28 问题描述: 问题求解: 判定方式就是采用层序遍历,对于一个完全二叉树来说,访问每个非空节点之前都不能访问过null. public boolean isComp ...

  9. Leetcode 110 Balanced Binary Tree 二叉树

    判断一棵树是否是平衡树,即左右子树的深度相差不超过1. 我们可以回顾下depth函数其实是Leetcode 104 Maximum Depth of Binary Tree 二叉树 /** * Def ...

随机推荐

  1. nginx的配置:目的是使用nginx反向代理后,应用程序获取用户真实ip

    一.了解nginx Nginx是lgor Sysoev为俄罗斯访问量第二的rambler.ru站点设计开发的.从2004年发布至今,凭借开源的力量,已经接近成熟与完善. Nginx功能丰富,可作为HT ...

  2. 尚学python课程---11、linux环境下安装python注意

    尚学python课程---11.linux环境下安装python注意 一.总结 一句话总结: 准备安装依赖包:zlib.openssl:yum install zlib* openssl*:pytho ...

  3. com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: Data source rejected establishment of connection, message from server: "Too many connections"

    报错: com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: Data source rejected estab ...

  4. C# GZipStream 压缩 解压

    关于GZipStream压缩解压,网上找了很多资料,完整的不多,要么是针对字符串压缩解压缩的,要么只实现了针对单个文件的压缩解压缩,还有的不支持子文件夹的压缩,实用性都不是很大. 以下整理了压缩解压缩 ...

  5. 函数开始处的MOV EDI, EDI的作用

    调试程序调试到系统库函数的代码时,总会发现系统函数都是从一条MOV EDI, EDI指令开始的,紧接着这条指令下面才是标准的建立函数局部栈的代码.对系统DLL比如ntdll.dll进行反汇编,可以发现 ...

  6. work-record:20190618

    ylbtech-work-record:20190618 1.返回顶部 1.1. -- formId记录表 -- select * from record_form_id; -- drop table ...

  7. Docker系列(五):Docker网络机制(上)

    Linux路由机制打通网络 路由机制是效率最好的 docker128上修改Docker0的网络地址,与docker130不冲突 vi /usr/lib/systemd/system/docker.se ...

  8. ASP.NET的底层体系1

    文章引导 1.ASP.NET的底层体系1 2.ASP.NET的底层体系2 引言: 学习ASP.NET,要想做的更好,不了解ASP.NET是不行的.一个有理想的程序员都会像挤压海绵一样,努力的去学习和获 ...

  9. DXP 笔记

    1. 从原理图上添加 net class,快捷键 : P -> V -> C

  10. CentOS中GDB提示Missing separate debuginfos解决办法

    安装debuginfo 修改文件 vi /etc/yum.repo.d/CentOS-Debuginfo.repo 修改enabled的值为1 使用debuginfo-install安装需要的文件