当前位置:网站首页>Niuke topic -- judge whether it is a complete binary tree or a balanced binary tree
Niuke topic -- judge whether it is a complete binary tree or a balanced binary tree
2022-07-27 16:57:00 【zhangzhang_ one】
List of articles
subject 1—— Determine whether it is a complete binary tree
Given a binary tree , Determine if it is a complete binary tree .
The definition of a complete binary tree : If the depth of the binary tree is h, In addition to h Out of layer , The number of nodes in other layers reaches the maximum , The first h All leaf nodes of layer are continuously concentrated on the leftmost , This is a completely binary tree .
The following example figure 1、2 It's a perfect binary tree , And the sample diagram 3 No .
Their thinking
In a complete binary tree, leaf nodes can only appear in the lowest and lower levels , And the lowest leaf nodes are concentrated in the left part of the tree .
The specific methods are as follows :
- First, judge that the empty tree must be a complete binary tree ;
- Initialize a queue auxiliary level traversal , And join the root node into the team ;
- Gradually visit the first node of the team , If a node is empty , Then mark , Represents the lowest level of a complete binary tree , If there are subsequent nodes that are not empty , It does not conform to the properties of a complete binary tree ;
- otherwise , Continue to add the left and right child nodes to the queue , Waiting for access .
Code implementation
public class Solution {
public boolean isCompleteTree (TreeNode root) {
if(root == null) return true;
boolean complete = false;
Queue<TreeNode> q = new LinkedList<>();
q.add(root);
while(!q.isEmpty()){
// Gradually visit the first element of the team
TreeNode cur = q.poll();
// If the team leader element is empty , Indicates that the empty node is encountered for the first time , marked
if(cur == null){
complete = true;
continue;
}
// After the empty node , And nodes , Does not conform to the nature of binary tree
if(complete) return false;
// None of the above is satisfied , Queue the left and right child nodes of the current node , Waiting for access
q.add(cur.left);
q.add(cur.right);
}
return true;
}
}
subject 2—— Determine whether it is a balanced binary tree
Given the number of nodes of a tree is n Two fork tree , Determine whether the binary tree is a balanced binary tree .
Properties of balanced binary trees : It is an empty tree or the absolute value of the height difference between its left and right subtrees does not exceed 1, And the left and right subtrees are both balanced binary trees .
The following example is a balanced binary tree .
Their thinking
According to the definition , If we can find the height of the tree , Then, the absolute value of the height difference between the left and right subtrees is less than or equal to 1, We can judge whether the tree with each node as the root meets the definition .
The specific methods are as follows :
- First, find the height of the subtree around the root node , And judge whether the absolute value of the height difference does not exceed 1;
- Recursively judge whether the left subtree and the right subtree are balanced binary trees ;
- When the three conditions are met at the same time , Balanced binary tree .
Code implementation
public class Solution {
// Solve the height of the tree
public int getHeight(TreeNode node){
int height = 0;
if(node == null) return height;
int l = getHeight(node.left);
int r = getHeight(node.right);
return Math.max(l,r)+1;
}
public boolean IsBalanced_Solution(TreeNode root) {
if(root == null) return true;
int l = getHeight(root.left);
int r = getHeight(root.right);
// The difference in height is greater than 1, It's not an equilibrium binary tree
if(Math.abs(l-r)>1) return false;
return IsBalanced_Solution(root.left) && IsBalanced_Solution(root.right);
}
}
边栏推荐
- Pdf extract text
- Fast Planner - detailed explanation of kinetic astar
- Automatic classification of e-commerce UGC pictures using Baidu PaddlePaddle easydl
- Storage of data in C language
- android中的图片三级缓存
- Json数据的格式使用
- Apache
- LOJ 510 - "libreoj noi round 1" memories outside the north school gate [line segment tree]
- Opencv (III) -- image segmentation
- Stylelint check error: unexpected missing generic font family font family no missing generic family keyword
猜你喜欢

Rotate the whole model in ADAMS

Apache
mvc和mvp和mvvm的区别

C语言之函数
![[paper reading] single- and cross modality near duplicate image pairsdetection via spatial transformer compare](/img/33/8af12d58f4afeb807ebf9a90a2ea47.png)
[paper reading] single- and cross modality near duplicate image pairsdetection via spatial transformer compare

Cvxpy - latest issue

Opencv (V) -- moving target recognition

【论文阅读】Single- and Cross-Modality Near Duplicate Image PairsDetection via Spatial Transformer Compar

Apache
The difference between MVC and MVP and MVVM
随机推荐
The difference between MVC and MVP and MVVM
Opencv (II) -- basic image processing
[paper reading] a CNN transformer hybrid approach for coding visual neuralactivity into text
android中的图片三级缓存
Json数据的格式使用
实测:云RDS MySQL性能是自建的1.6倍
被动收入:回归原始且安全的两种赚取方法
training on multiple GPUs pytorch
Share a scheme of "redis" to realize "chat round system" that can't be found on the Internet
Opencv (IV) -- image features and target detection
Jerry's maximum volume prompt sound cannot be broadcast [article]
String numeric type converted to thousands
京东张政:内容理解在广告场景下的实践和探索
Storage of data in C language
Exe program encryption lock
Automatic classification of e-commerce UGC pictures using Baidu PaddlePaddle easydl
JD Zhang Zheng: practice and exploration of content understanding in advertising scenes
jsp-El表达式,JSTL标签
密码学系列之:PKI的证书格式表示X.509
Get the array list of the previous n days and the previous and subsequent days of the current time, and cycle through each day