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

边栏推荐
- Go RPC call
- Win:将一般用户添加到 Local Admins 组中
- Runc hang causes the kubernetes node notready
- When to catch an exception and when to throw an exception- When to catch the Exception vs When to throw the Exceptions?
- 官宣!第三届云原生编程挑战赛正式启动!
- Interesting practice of robot programming 14 robot 3D simulation (gazebo+turtlebot3)
- Limited query of common SQL operations
- Mysql database | build master-slave instances of mysql-8.0 or above based on docker
- 85.4% mIOU! NVIDIA: using multi-scale attention for semantic segmentation, the code is open source!
- One plus six brushes into Kali nethunter
猜你喜欢

Do you know the eight signs of a team becoming agile?

Openresty ngx Lua Execution stage

The MySQL team development specifications used by various factories are too detailed. It is recommended to collect them!

Go RPC call

力扣剑指offer——二叉树篇

Introduce reflow & repaint, and how to optimize it?

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!

Win: use shadow mode to view the Desktop Session of a remote user

Action News

Codeforces Global Round 19 ABC
随机推荐
Prometheus monitors the correct posture of redis cluster
Summary of regularization methods
How to find hot projects in 2022? Dena community project progress follow-up, there is always a dish for you (1)
R language uses logistic regression and afrima, ARIMA time series models to predict world population
Yyds dry inventory jetpack hit dependency injection framework Getting Started Guide
A tab Sina navigation bar
The application and Optimization Practice of redis in vivo push platform is transferred to the end of metadata by
Word processing software
Kotlin - 协程 Coroutine
Luo Gu Pardon prisoners of war
Win: use shadow mode to view the Desktop Session of a remote user
Matrixone 0.2.0 is released, and the fastest SQL computing engine is coming
JVM - when multiple threads initialize the same class, only one thread is allowed to initialize
Interesting practice of robot programming 16 synchronous positioning and map building (SLAM)
Tla+ through examples (XI) -- propositional logic and examples
openresty ngx_lua執行階段
力扣剑指offer——二叉树篇
WCF: expose unset read-only DataMember property- WCF: Exposing readonly DataMember properties without set?
Asynchronous and promise
Restful Fast Request 2022.2.1发布,支持cURL导入