当前位置:网站首页>【LeetCode】110. Balanced binary tree (2 brushes of wrong questions)
【LeetCode】110. Balanced binary tree (2 brushes of wrong questions)
2022-07-05 02:20:00 【Kaimar】



- Ideas
That is, the height difference between the left and right subtrees is not higher than 1 that will do , It needs to be seen from the bottom up , So we use the method of post order traversal , Use post order traversal recursion , Then calculate the height difference between the left and right subtrees , If not, return directly false, If you are satisfied at the top, you will return true.
Recursive Trilogy
Recursive parameters and return values : The parameter is the current node , The return value is the height difference between the left and right subtrees of the current node ;
The termination condition of recursion : Return when empty true, When the height difference between the left and right subtrees is greater than 1 When to return to false;
Recursive single-layer logic : Use post order traversal , Then it is the traversal order in left and right , Enter the next floor left and right , And return . - problem
There is a problem in the middle of this inscription , That is, the returned value does not know which one to return , If not, return -1, Then who is satisfied to return ?
So I refer to the solution , Find that the thought is correct ,This is actually the height , It needs to be followed from bottom to top , If it's depth , From top to bottom is the preface ,Then for meeting the conditions , Returns the larger height in the left and right subtrees +1.
/** * Definition for a binary tree node. * type TreeNode struct { * Val int * Left *TreeNode * Right *TreeNode * } */
func max(a, b int) int {
if a >= b {
return a
}
return b
}
func height(root *TreeNode) int {
if root == nil {
return 0
}
// Left
leftHeight := height(root.Left)
if leftHeight == -1 {
return -1
}
// Right
rightHeight := height(root.Right)
if rightHeight == -1 {
return -1
}
// in
var res int
if leftHeight - rightHeight > 1 || rightHeight - leftHeight > 1 {
res = -1
} else {
res = 1 + max(leftHeight, rightHeight)
}
return res
}
func isBalanced(root *TreeNode) bool {
if root == nil {
return true
}
if height(root) == -1 {
return false
}
return true
}

边栏推荐
- One click generation and conversion of markdown directory to word format
- How to make a cool ink screen electronic clock?
- Codeforces Round #770 (Div. 2) ABC
- R语言用logistic逻辑回归和AFRIMA、ARIMA时间序列模型预测世界人口
- The perfect car for successful people: BMW X7! Superior performance, excellent comfort and safety
- Timescaledb 2.5.2 release, time series database based on PostgreSQL
- A label making navigation bar
- R language uses logistic regression and afrima, ARIMA time series models to predict world population
- 85.4% mIOU! NVIDIA: using multi-scale attention for semantic segmentation, the code is open source!
- Stored procedure and stored function in Oracle
猜你喜欢

Open source SPL optimized report application coping endlessly

Five ways to query MySQL field comments!
![[technology development-26]: data security of new information and communication networks](/img/13/10c8bd340017c6516edef41cd3bf6f.png)
[technology development-26]: data security of new information and communication networks

The steering wheel can be turned for one and a half turns. Is there any difference between it and two turns

R language uses logistic regression and afrima, ARIMA time series models to predict world population

Do you know the eight signs of a team becoming agile?
![[技术发展-26]:新型信息与通信网络的数据安全](/img/13/10c8bd340017c6516edef41cd3bf6f.png)
[技术发展-26]:新型信息与通信网络的数据安全

He was laid off.. 39 year old Ali P9, saved 150million

Comment mettre en place une équipe technique pour détruire l'entreprise?

Visual explanation of Newton iteration method
随机推荐
Chinese natural language processing, medical, legal and other public data sets, sorting and sharing
When to catch an exception and when to throw an exception- When to catch the Exception vs When to throw the Exceptions?
Talk about the things that must be paid attention to when interviewing programmers
WCF: expose unset read-only DataMember property- WCF: Exposing readonly DataMember properties without set?
Data guard -- theoretical explanation (III)
Application and development trend of image recognition technology
The steering wheel can be turned for one and a half turns. Is there any difference between it and two turns
Official announcement! The third cloud native programming challenge is officially launched!
Rabbit MQ message sending of vertx
Outlook: always prompt for user password
[Digital IC hand tearing code] Verilog edge detection circuit (rising edge, falling edge, double edge) | topic | principle | design | simulation
openresty ngx_lua变量操作
Educational Codeforces Round 122 (Rated for Div. 2) ABC
One click generation and conversion of markdown directory to word format
Application and Optimization Practice of redis in vivo push platform
Yolov5 model training and detection
Richview trvunits image display units
[機緣參悟-38]:鬼穀子-第五飛箝篇 - 警示之一:有一種殺稱為“捧殺”
Kotlin - 协程 Coroutine
Pytorch register_ Hook (operate on gradient grad)