当前位置:网站首页>【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
}
边栏推荐
- STL container
- A tab Sina navigation bar
- He was laid off.. 39 year old Ali P9, saved 150million
- 172. Zero after factorial
- Learn game model 3D characters, come out to find a job?
- WCF: expose unset read-only DataMember property- WCF: Exposing readonly DataMember properties without set?
- Last week's hot review (2.7-2.13)
- Prometheus monitors the correct posture of redis cluster
- spoon插入更新oracle数据库,插了一部分提示报错Assertion botch: negative time
- 179. Maximum number - sort
猜你喜欢
A label making navigation bar
Traditional chips and AI chips
[source code attached] Intelligent Recommendation System Based on knowledge map -sylvie rabbit
Application and Optimization Practice of redis in vivo push platform
LeetCode 314. Binary tree vertical order traversal - Binary Tree Series Question 6
Grub 2.12 will be released this year to continue to improve boot security
STM32 series - serial port UART software pin internal pull-up or external resistance pull-up - cause problem search
Official announcement! The third cloud native programming challenge is officially launched!
Yolov5 model training and detection
openresty ngx_lua执行阶段
随机推荐
[illumination du destin - 38]: Ghost Valley - chapitre 5 Flying clamp - one of the Warnings: There is a kind of killing called "hold Kill"
Yyds dry inventory swagger positioning problem ⽅ formula
Write a thread pool by hand, and take you to learn the implementation principle of ThreadPoolExecutor thread pool
Advanced conditional statements of common SQL operations
One plus six brushes into Kali nethunter
Yolov5 model training and detection
Interesting practice of robot programming 16 synchronous positioning and map building (SLAM)
[機緣參悟-38]:鬼穀子-第五飛箝篇 - 警示之一:有一種殺稱為“捧殺”
Yolov5 model training and detection
Valentine's Day flirting with girls to force a small way, one can learn
CAM Pytorch
One plus six brushes into Kali nethunter
Interesting practice of robot programming 15- autoavoidobstacles
pytorch fine-tuning (funtune) : 镂空设计or 偷梁换柱
Exploration of short text analysis in the field of medical and health (I)
Icu4c 70 source code download and compilation (win10, vs2022)
Redis' hyperloglog as a powerful tool for active user statistics
When to catch an exception and when to throw an exception- When to catch the Exception vs When to throw the Exceptions?
Learn tla+ (XII) -- functions through examples
A label making navigation bar