当前位置:网站首页>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
边栏推荐
- Ouvrir des contrats à terme, ouvrir des comptes en nuage ou faire confiance aux logiciels des sociétés à terme?
- 读《Software Engineering at Google》(15)
- C# Task. Delay and thread The difference between sleep
- How to change input into text
- Baidu Map 3D rotation and tilt angle adjustment
- Detailed explanation of C webpai route
- El cascade and El select click elsewhere to make the drop-down box disappear
- Basic case of Baidu map
- Read software engineering at Google (15)
- Bottom processing of stack memory in browser
猜你喜欢
Collection of common SQL statements
【生活中的逻辑谬误】稻草人谬误和无力反驳不算证明
Allowed latency and side output
EF core in ASP Generate core priority database based on net entity model
RPC核心概念理解
uni-app黑马优购项目学习记录(下)
.Net Core3. 1 use razorengine NETCORE production entity generator (MVC web version)
Learning record of uni app dark horse yougou project (Part 2)
Perception of linear algebra 2
JS, entries(), keys(), values(), some(), object Assign() traversal array usage
随机推荐
El date picker limits the selection range from the current time to two months ago
Promise (II)
uni-app黑马优购项目学习记录(下)
The system cannot be started after AHCI is enabled
How to change input into text
ASP. Net core configuration options (Part 2)
Summary of common SQL statements
In ancient Egypt and Greece, what base system was used in mathematics
基于51单片机红外无线通讯仿真
01-初识sketch-sketch优势
Understanding and small examples of unity3d object pool
stm32入门开发板选野火还是正点原子呢?
1-3 nodejs installation list configuration and project environment
Deep understanding of control inversion and dependency injection
Flash project cross domain interception and DBM database learning [Baotou cultural and creative website development]
Freecodecamp ---- budget & category exercise
How to sort the numbers with text in Excel from small to large instead of the first number
Bottom processing of stack memory in browser
Detailed explanation of C webpai route
freeCodeCamp----prob_ Calculator exercise