当前位置:网站首页>判断一颗树是否为完全二叉树——视频讲解!!!
判断一颗树是否为完全二叉树——视频讲解!!!
2022-07-30 09:15:00 【学习追求高效率】
7. (视频讲解)判断一棵树 是否为 完全二叉树
判断一棵树是否为完全二叉树
注意:队列中 元素也可以是 null
// 判断一棵树是不是完全二叉树
boolean isCompleteTree(TreeNode root) {
if(root == null) return true;
Queue<TreeNode> queue = new LinkedList<>();
queue.offer(root);
while (!queue.isEmpty()) {
TreeNode cur = queue.poll();
if(cur != null) {
queue.offer(cur.left);
queue.offer(cur.right);
}else {
break;
}
}
while (!queue.isEmpty()) {
TreeNode cur = queue.peek();
if(cur != null) {
//不是满二叉树
return false;
}else {
queue.poll();
}
}
return true;
}
总结
- 根据二叉树的性质 — 子树 的位置 必须从左到右,依次排序
- 用栈 保存
边栏推荐
猜你喜欢

连接mysql报错WARN: Establishing SSL connection without server‘s identity verification is not recommended

宝塔搭建DM企业建站系统源码实测

详解JVM垃圾回收

2022 Hangzhou Electric Multi-School 2nd Game

分布式系统大势所趋,银行运维如何与时俱进?

69. Sqrt(x)x 的平方根

Apache DolphinScheduler新一代分布式工作流任务调度平台实战-上

涛思 TDengine 2.6+优化参数

Test automation selenium (a)

怎么在本地电脑上运行dist文件
随机推荐
虚幻引擎图文笔记:could not be compiled. Try rebuilding from source manually.问题的解决
20220728使用电脑上的蓝牙和汇承科技的蓝牙模块HC-05配对蓝牙串口传输
怎么在本地电脑上运行dist文件
积分专题笔记-积分的定义
LeetCode二叉树系列——94.二叉树的中序遍历
涛思 TDengine 2.6+优化参数
echart图表清空上一次数据
柱状图 直方图 条形图 的区别
Concise Notes on Integrals - Types of Curve Integrals of the First Kind
Apache DolphinScheduler新一代分布式工作流任务调度平台实战-上
无法定位程序输入点ucrtbase.abort于动态链接库api-ms-win-crt-runtime-|1-1-0.dll上
Taosi TDengine 2.6+ optimization parameters
342 · 山谷序列
利用R语言读取csv文件入一个数据框,然后查看各列的属性。
2022/07/29 Study Notes (day19) Exception Handling
反射技巧让你的性能提升 N 倍
The sword refers to offer 48: the longest non-repeating substring
iperf3 参数选项详细说明
2022 Hangzhou Electric Multi-School 2nd Game
Access to display the data