当前位置:网站首页>Determine whether a tree is a complete binary tree - video explanation!!!
Determine whether a tree is a complete binary tree - video explanation!!!
2022-07-30 09:54:00 【Learning to pursue high efficiency】
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;
}
总结
- 根据二叉树的性质 — 子树 的位置 必须从左到右,依次排序
- 用栈 保存
边栏推荐
猜你喜欢
Re18:读论文 GCI Everything Has a Cause: Leveraging Causal Inference in Legal Text Analysis
MySQL【运算符】
Test automation selenium (a)
ospf2双点双向重发布(题2)
Unified exception handling causes ResponseBodyAdvice to fail
Day113.尚医通:微信登录二维码、登录回调接口
功能测试、UI自动化测试(web自动化测试)、接口自动化测试
qsort 函数的使用及其模拟实现
leetcode 剑指 Offer 12. 矩阵中的路径
ClickHouse
随机推荐
leetcode 剑指 Offer 63. 股票的最大利润
shell script
PyTorch安装及环境配置(Win10)
MySQL之COUNT性能到底如何?
Use the R language to read the csv file into a data frame, and then view the properties of each column.
ClickHouse
CSDN21天学习挑战赛
Taosi TDengine 2.6+ optimization parameters
How to run dist file on local computer
使用 Neuron 接入 Modbus TCP 及 Modbus RTU 协议设备
MySQL【运算符】
PyQt5-在窗口上绘制文本
包、类及四大权限和static
Shell系统学习之数组
Integral Special Notes - Definition of Integral
Unity性能分析 Unity Profile性能分析工具
新一代开源免费的终端工具,太酷了
Access to display the data
国外资源加速下载器,代码全部开源
Apache DolphinScheduler新一代分布式工作流任务调度平台实战-上