当前位置:网站首页>【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
}

边栏推荐
- The perfect car for successful people: BMW X7! Superior performance, excellent comfort and safety
- Erreur de type de datagramme MySQL en utilisant Druid
- 如何做一个炫酷的墨水屏电子钟?
- [download white paper] does your customer relationship management (CRM) really "manage" customers?
- Three properties that a good homomorphic encryption should satisfy
- One plus six brushes into Kali nethunter
- Rabbit MQ message sending of vertx
- Educational Codeforces Round 122 (Rated for Div. 2) ABC
- PowerShell: use PowerShell behind the proxy server
- Numpy library introductory tutorial: basic knowledge summary
猜你喜欢

One plus six brushes into Kali nethunter

A tab Sina navigation bar

Interesting practice of robot programming 16 synchronous positioning and map building (SLAM)

Lsblk command - check the disk of the system. I don't often use this command, but it's still very easy to use. Onion duck, like, collect, pay attention, wait for your arrival!

Go RPC call

PowerShell: use PowerShell behind the proxy server

Android advanced interview question record in 2022

The perfect car for successful people: BMW X7! Superior performance, excellent comfort and safety
![[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

Practice of tdengine in TCL air conditioning energy management platform
随机推荐
Last week's hot review (2.7-2.13)
Flutter 2.10 update details
Learn tla+ (XII) -- functions through examples
Grub 2.12 will be released this year to continue to improve boot security
使用druid連接MySQL數據庫報類型錯誤
PowerShell: use PowerShell behind the proxy server
The most powerful new household god card of Bank of communications. Apply to earn 2100 yuan. Hurry up if you haven't applied!
R language uses logistic regression and afrima, ARIMA time series models to predict world population
[source code attached] Intelligent Recommendation System Based on knowledge map -sylvie rabbit
The perfect car for successful people: BMW X7! Superior performance, excellent comfort and safety
Redis' hyperloglog as a powerful tool for active user statistics
Exploration of short text analysis in the field of medical and health (II)
Can you really learn 3DMAX modeling by self-study?
[technology development-26]: data security of new information and communication networks
Asynchronous and promise
Win:使用 Shadow Mode 查看远程用户的桌面会话
Kotlin - 协程 Coroutine
February database ranking: how long can Oracle remain the first?
openresty ngx_lua执行阶段
[技术发展-26]:新型信息与通信网络的数据安全