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

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

若设二叉树的深度为 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. CF475F meta-universe

    题意:给你一个无限大矩形中有一些planet,每次可以选择某一没有planet的行或列分割开矩形(分割开以后要求矩形不为空).问最后能分割成几个矩形? 标程: #include<bits/std ...

  2. The Preliminary Contest for ICPC Asia Nanjing 2019 C. Tsy's number 5

    https://nanti.jisuanke.com/t/41300 题意:求\(\sum_{i=1}^n\phi(i)\phi(j)2^{\phi(i)\phi(j)}\) \(f_i=\sum_{ ...

  3. 线性推概率——cf1009E好题!

    依次求每一段公里的期望消耗即可,这是可以递推的 dp[i]表示每公里的期望消耗 dp[i]=1/2*a1+1/4*a2 +...+1/2^(i-1)*ai-1 + 1/2^(i-1)*ai注意最后一项 ...

  4. hdu多校第一场1003 (hdu6580)Milk 背包

    题意: 有一个n*m的矩阵,左右可以随便走,但只能在每一行的中点往下走,每走一格花费时间1. 现在这个矩阵里放了k瓶牛奶,第i个牛奶喝下去需要ti时间 起点是(1,1) 对于每个i∈[1,k],问喝掉 ...

  5. el-table单元格新增、编辑、删除功能

    <template> <div class="box"> <el-button class="addBtn" type=" ...

  6. JAVA 设计模式之 工厂模式详解

    一.简单工厂模式 简单工厂模式(Simple Factory Pattern)是指由一个工厂对象决定创建出哪一种产品类 的实例.属于创建型模式,但它不属于 GOF,23 种设计模式 (参考资料: ht ...

  7. 在centos 6.9 x64下安装code::blocks步骤

    1.yum groupinstall "Development tools" 2.yum install gtk2* 3.安装wxWidgets 下载地址:https://www. ...

  8. 生成器yield(17-06)

    yield  执行以上代码,yield后面可以有返回值 next() 获取 next的使用次数,是你生成器中yield出现的次数 def p(): print("ok") yiel ...

  9. 深入浅出Java中的clone克隆方法,写得太棒了!

    作者:张纪刚 blog.csdn.net/zhangjg_blog/article/details/18369201/ Java中对象的创建 clone 顾名思义就是 复制 , 在Java语言中, c ...

  10. <每日一题>算法题:小球的下落距离

    题目:小东和三个朋友一起在楼上抛小球,他们站在楼房的不同层,假设小东站的楼层距离地面N米,球从他手里自由落下,每次落地后反跳回上次下落高度的一半,并以此类推知道全部落到地面不跳,求4个小球一共经过了多 ...