当前位置:网站首页>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
边栏推荐
- Further optimize Baidu map data visualization
- Use of shell sed command
- Excel quickly and automatically fills the contents of a row on a blank cell
- Ouvrir des contrats à terme, ouvrir des comptes en nuage ou faire confiance aux logiciels des sociétés à terme?
- Metaprogramming, proxy and reflection
- Shell-awk命令的使用
- 为什么有些人说单片机简单,我学起来这么吃力?
- Scope and scope chain in JS
- uni-app黑马优购项目学习记录(下)
- Webapi + form form upload file
猜你喜欢

Why do some people say SCM is simple and I have to learn it so hard?

线性代数感悟之1

Bottom processing of stack memory in browser

练习:求偶数和、阈值分割和求差( list 对象的两个基础小题)

1-1 NodeJS

XTask与Kotlin Coroutine的使用对比

Qt error: /usr/bin/ld: cannot find -lGL: No such file or directory
![[difference between Oracle and MySQL]](/img/90/6d030a35692fa27f1a7c63985af06f.png)
[difference between Oracle and MySQL]

Qt error: /usr/bin/ld: cannot find -lGL: No such file or directory
MySQL installation
随机推荐
Promise (IV)
Future 用法详解
JS, entries(), keys(), values(), some(), object Assign() traversal array usage
Use of five routing guards
Double pointer advanced -- leetcode title -- container with the most water
Self use learning notes - connectingstring configuration
Qt error: /usr/bin/ld: cannot find -lGL: No such file or directory
Preliminary understanding of promse
Shell-入门、变量、以及基本的语法
[ES6] promise related (event loop, macro / micro task, promise, await / await)
The system cannot be started after AHCI is enabled
JS to find the character that appears three times in the string
ASP. Net core reads the configuration file in the class library project
[difference between Oracle and MySQL]
Wiper component encapsulation
. net cross platform principle (Part I)
[related to zhengheyuan cutting tools]
Qt error: /usr/bin/ld: cannot find -lGL: No such file or directory
Read software engineering at Google (15)
Using quartz under. Net core -- preliminary understanding of [2] operations and triggers