当前位置:网站首页>958. 二叉树的完全性检验
958. 二叉树的完全性检验
2022-04-23 17:32:00 【hequnwang10】
一、题目描述
示例 1:
输入:root = [1,2,3,4,5,6]
输出:true
解释:最后一层前的每一层都是满的(即,结点值为 {1} 和 {2,3} 的两层),且最后一层中的所有结点({4,5,6})都尽可能地向左。
示例 2:
输入:root = [1,2,3,4,5,null,7]
输出:false
解释:值为 7 的结点没有尽可能靠向左侧。
二、解题
DFS
当前节点N的左子节点的值为2N,右子节点的值为2N+1,每次遍历时统计节点数,和节点的最大值。如果满足完全二叉树,则节点数和最大值相等,否则不相等
class Solution {
//最大值
int maxNum = 0;
//计数节点
int cnt = 0;
public boolean isCompleteTree(TreeNode root) {
//DFS 根节点为N ,左子节点2*N 右子节点 2*N+1;
if(root == null){
return true;
}
dfs(root,1);
return maxNum == cnt;
}
public void dfs(TreeNode root,int curNum){
//终止条件
if(root == null){
return;
}
maxNum = Math.max(maxNum,curNum);
cnt++;
dfs(root.left,2*curNum);
dfs(root.right,2*curNum+1);
}
}
BFS
广度优先遍历。当出现 null 值时停止遍历,如果此时还有没有遍历到的结点,说明该树非完全二叉树。
class Solution {
// 广度遍历二叉树,当出现 null 值时停止遍历,如果此时还有没有遍历到的结点,说明该树非完全二叉树。
public boolean isCompleteTree(TreeNode root) {
Deque<TreeNode> queue = new LinkedList<>();
TreeNode cur;
queue.add(root);
while((cur = queue.poll()) != null){
queue.add(cur.left);
queue.add(cur.right);
}
//判断队列中是否为空
while(!queue.isEmpty()){
if(queue.poll() != null){
return false;
}
}
return true;
}
}
版权声明
本文为[hequnwang10]所创,转载请带上原文链接,感谢
https://blog.csdn.net/hequnwang10/article/details/124331496
边栏推荐
- PC电脑使用无线网卡连接上手机热点,为什么不能上网
- MySQL installation
- Some problems encountered in recent programming 2021 / 9 / 8
- 1217_使用SCons生成目标文件
- 常用SQL语句总结
- For the space occupation of the software, please refer to the installation directory
- EF core in ASP Generate core priority database based on net entity model
- 古代埃及希腊,数学用的什么进制
- Why do some people say SCM is simple and I have to learn it so hard?
- El date picker limits the selection range from the current time to two months ago
猜你喜欢
Scope and scope chain in JS
Why do some people say SCM is simple and I have to learn it so hard?
JS, entries(), keys(), values(), some(), object Assign() traversal array usage
Learning record of uni app dark horse yougou project (Part 2)
Collection of common SQL statements
Qt error: /usr/bin/ld: cannot find -lGL: No such file or directory
In embedded system, must the program code in flash be moved to ram to run?
01-初识sketch-sketch优势
PC电脑使用无线网卡连接上手机热点,为什么不能上网
Signalr can actively send data from the server to the client
随机推荐
Perception of linear algebra 2
[C#] 彻底搞明白深拷贝
Baidu Map Case - Zoom component, map scale component
Oninput one function to control multiple oninputs (take the contents of this input box as parameters) [very practical, very practical]
uni-app黑马优购项目学习记录(下)
[logical fallacy in life] Scarecrow fallacy and inability to refute are not proof
ASP. Net core dependency injection service life cycle
HCIP第五次实验
[simple understanding of database]
QT modification UI does not take effect
线性代数感悟之2
Clickhouse SQL operation
El date picker limits the selection range from the current time to two months ago
Learning record of uni app dark horse yougou project (Part 2)
JVM class loading mechanism
Using quartz under. Net core - calendar of [6] jobs and triggers
双指针进阶--leetcode题目--盛最多水的容器
Shell-入门、变量、以及基本的语法
2. Electron's HelloWorld
Exercise: even sum, threshold segmentation and difference (two basic questions of list object)