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 ...
随机推荐
- BCZM : 1.6
https://blog.csdn.net/kabini/article/details/2311946 题目大意: 水房能容纳饮料的总量是V,有一批饮料,每种饮料单个容量都是2的方幂,每种饮料信息如 ...
- [JZOJ3302] 【集训队互测2013】供电网络
题目 题目大意 给你一个有向图,每个点开始有一定的水量(可能为负数),可以通过边流到其它点. 每条边的流量是有上下界的. 每个点的水量可以增加或减少(从外界补充或泄出到外界),但是需要费用,和增加(减 ...
- Spring Boot 成长之路(一) 快速上手
1.创建工程 利用IntelliJ IDEA新建一个Spring Boot项目的Web工程 2.查看初始化的spring boot项目 工程建好之后会出现如下的目录结构: 值得注意的第一件事是,整个项 ...
- kkFileView在centos7上安装
kkFileView是使用spring boot打造文件文档在线预览项目解决方案. 项目地址:https://gitee.com/kekingcn/file-online-preview 安装步骤: ...
- (转)java源程序加密解决方案(基于Classloader解密)
转:http://cjnetwork.iteye.com/blog/851544 源程序加密解决方案 1. 概述: Java源程序的加密,有如下两种: 1使用混淆器对源码进行混淆,降低反编译工具的作用 ...
- SpringCloud学习笔记(七):Hystrix断路器
概述 什么时候需要断路器?熔断? 举个简单的例子:小明喜欢小美,可是小美没有电话,小美给了小明家里的座机,小明打给座机,这个时候小美的妈妈接到了,小明怕妈妈知道自己喜欢小美,就跟小美妈妈说让小美哥接电 ...
- 详解Android广播机制
应用场景(常见的场景1) (1)同一应用具有多个进程的不同组件之间的消息通信 a)不同应用间的组件之间的消息通信 b)与Android系统在特定情况下的通信,如:系统开机,网络变化等 (2)同一应用内 ...
- python三元运算符公式/出错怎么看
成功 if 条件 else 失败 Tip:问题要从下往上看,出问题的是最底下的问题,
- UNIT对话系统(杂记)
单轮对话指标: 召回率=机器人能回答的问题数/问题总数 准确率=机器人正确回答的问题数/问题总数 问题解决率=机器成功解决的问题数/问题总数 多轮对话指标: 任务完成率=成功结束的多轮会话数/多轮会话 ...
- Caffe系列4——基于Caffe的MNIST数据集训练与测试(手把手教你使用Lenet识别手写字体)
基于Caffe的MNIST数据集训练与测试 原创:转载请注明https://www.cnblogs.com/xiaoboge/p/10688926.html 摘要 在前面的博文中,我详细介绍了Caf ...