当前位置:网站首页>[leetcode] balanced binary tree
[leetcode] balanced binary tree
2022-06-11 01:50:00 【xiaoshijiu333】
#LeetCode A daily topic 【 Special topic of binary tree 】
- Balanced binary trees
https://leetcode-cn.com/problems/balanced-binary-tree/ - analysis
The definition of balanced binary tree :
The absolute value of the height difference between the left and right subtrees of each node does not exceed 1, That is to say, both the left and right subtrees of a balanced binary tree are balanced binary trees , And the absolute value of the height difference between the left and right subtrees does not exceed 1;
When finding the height of a tree , Use dfs Recursion , Knowing the height of the left and right subtrees can also determine whether the tree is balanced , If a subtree is unbalanced , Then the whole tree is unbalanced . - Realization
public boolean isBalanced(TreeNode root) {
// It's not equal to -1, Namely balance ( And the return value is the height of the tree )
return height(root) != -1;
}
// Since this method returns the height of the tree , At the same time, it can also judge whether the tree is a balanced binary tree
// Be flexible , When the tree is unbalanced, it returns directly to -1( That is, the height difference >1), One of the word trees is unbalanced , The tree must be unbalanced
private int height(TreeNode root) {
if (root == null) {
return 0;
}
int left = heightOptimize(root.left);
int right = heightOptimize(root.right);
// One of the trees is unbalanced , Return -1, out-off-balance
if (left == -1 || right == -1) {
return -1;
}
// The height difference is greater than 1, out-off-balance
if (Math.abs(left - right) > 1) {
return -1;
}
// normal , Balance returns to its height
return Math.max(left, right) + 1;
}
LeetCode Time consuming :0ms
- summary
When finding the height of a tree , Use dfs Recursion , Knowing the height of the left and right subtrees can also determine whether the tree is balanced , If a subtree is unbalanced , Then the whole tree is unbalanced
边栏推荐
猜你喜欢

Leetcode linked list queue stack problem
![[ROS introduction] cmakelist Txt and packages XML interpretation](/img/26/bae82a457fb83b2214d2f8c20955e2.jpg)
[ROS introduction] cmakelist Txt and packages XML interpretation
![[leetcode] LRU cache](/img/14/cedd5bb84ae1cceb62016d13e67f67.jpg)
[leetcode] LRU cache

Classic questions: 01 backpack, complete backpack, multiple backpack, two-dimensional cost Backpack

1.4px4 program download

视频压缩数据集TVD

Garbled code when the command parameter contains% in VisualStudio debugging

2.1、ROS+PX4仿真---定点飞行控制
![[recommended by Zhihu knowledge master] castle in UAV - focusing on the application of UAV in different technical fields](/img/c6/f1cec6de62e85de446dba7ea8675f0.jpg)
[recommended by Zhihu knowledge master] castle in UAV - focusing on the application of UAV in different technical fields
![[path planning] week 1: hodgepodge](/img/80/074b847c6826b306318aeb9866a829.jpg)
[path planning] week 1: hodgepodge
随机推荐
Detailed explanation of classic papers on OCR character recognition
Be careful, these hidden "bugs" of "array" to "collection"“
Loki 学习总结(1)—— Loki 中小项目日志系统的不二之选
2021-2-26 compilation of programming language knowledge points
Summary of SAS final review knowledge points (notes on Application of multivariate statistics experiment)
从解读 BDC 自动生成的代码谈起,讲解 SAPGUI 的程序组成部分试读版
Threejs: streamer effect encapsulation
懒汉式单例模式
Matlab array other common operation notes
PX4从放弃到精通(二十四):自定义机型
2.1 ros+px4 simulation - Fixed Point flight control
ROS parameter server
“看似抢票实际抢钱”,别被花式抢票产品一再忽悠
threejs:如何获得几何体的boundingBox?
A brief history of neural network
A tutorial on building a website from scratch with complete steps (7000 words and 102 screenshots for everyone to understand, with source code attached)
[leetcode] ordered linked list transformation binary search tree
"It looks like robbing tickets but actually robbing money". Don't be fooled by fancy ticket robbing products again and again
Dialog AlertDialog 、SimpleDialog、showModalBottomSheet、showToast Flutter 自定义 Dialog
PX4装机教程(六)垂起固定翼(倾转)