当前位置:网站首页>958. Complete binary tree test
958. Complete binary tree test
2022-04-23 17:33:00 【hequnwang10】
One 、 Title Description
Example 1:
Input :root = [1,2,3,4,5,6]
Output :true
explain : Every floor before the last floor is full ( namely , The node value is {1} and {2,3} The two layers ), And all nodes in the last layer ({4,5,6}) As far left as possible .
Example 2:
Input :root = [1,2,3,4,5,null,7]
Output :false
explain : The value is 7 The node of is not as far to the left as possible .
Two 、 Problem solving
DFS
Current node N The value of the left child node of is 2N, The value of the right child node is 2N+1, Count the number of nodes during each traversal , And the maximum value of the node . If a complete binary tree is satisfied , Then the number of nodes and the maximum value are equal , Otherwise it's not equal
class Solution {
// Maximum
int maxNum = 0;
// Counting node
int cnt = 0;
public boolean isCompleteTree(TreeNode root) {
//DFS The root node is N , The left child node 2*N The right child node 2*N+1;
if(root == null){
return true;
}
dfs(root,1);
return maxNum == cnt;
}
public void dfs(TreeNode root,int curNum){
// Termination conditions
if(root == null){
return;
}
maxNum = Math.max(maxNum,curNum);
cnt++;
dfs(root.left,2*curNum);
dfs(root.right,2*curNum+1);
}
}
BFS
Breadth first traversal . When there is a null Stop traversal on value , If there are nodes that have not been traversed at this time , It shows that the tree is not a complete binary tree .
class Solution {
// Breadth traversal binary tree , When there is a null Stop traversal on value , If there are nodes that have not been traversed at this time , It shows that the tree is not a complete binary tree .
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);
}
// Judge whether the queue is empty
while(!queue.isEmpty()){
if(queue.poll() != null){
return false;
}
}
return true;
}
}
版权声明
本文为[hequnwang10]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204231732009375.html
边栏推荐
- Baidu Map Case - Zoom component, map scale component
- Low code development platform sorting
- Open futures, open an account, cloud security or trust the software of futures companies?
- Summary of common SQL statements
- Collection of common SQL statements
- 122. 买卖股票的最佳时机 II-一次遍历
- Abnormal resolution of Xiaomi camera
- 【WPF绑定3】 ListView基础绑定和数据模板绑定
- Using quartz under. Net core - calendar of [6] jobs and triggers
- C# Task. Delay and thread The difference between sleep
猜你喜欢
常用SQL语句总结
Perception of linear algebra 2
基于51单片机红外无线通讯仿真
MySQL installation
.Net Core3. 1 use razorengine NETCORE production entity generator (MVC web version)
ASP. NET CORE3. 1. Solution to login failure after identity registers users
Compare the performance of query based on the number of paging data that meet the query conditions
Matlab / Simulink simulation of double closed loop DC speed regulation system
. net cross platform principle (Part I)
Devexpress GridView add select all columns
随机推荐
超分之TDAN
[difference between Oracle and MySQL]
Shell-sort命令的使用
ASP. Net core reads the configuration file in the class library project
读《Software Engineering at Google》(15)
Use of todesk remote control software
C# Task. Delay and thread The difference between sleep
Clickhouse SQL operation
Conversion between hexadecimal numbers
Detailed explanation of C webpai route
41. 缺失的第一个正数
Using quartz under. Net core - calendar of [6] jobs and triggers
C dapper basically uses addition, deletion, modification and query transactions, etc
Promise (II)
Using quartz under. Net core -- preliminary understanding of [2] operations and triggers
Abnormal resolution of Xiaomi camera
Halo 开源项目学习(二):实体类与数据表
Baidu Map 3D rotation and tilt angle adjustment
给 el-dialog 增加拖拽功能
Baidu Map Case - Zoom component, map scale component