当前位置:网站首页>【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
}
边栏推荐
- JVM - when multiple threads initialize the same class, only one thread is allowed to initialize
- RichView TRVStyle MainRVStyle
- 如何做一个炫酷的墨水屏电子钟?
- 官宣!第三届云原生编程挑战赛正式启动!
- RichView TRVUnits 图像显示单位
- Yyds dry inventory swagger positioning problem ⽅ formula
- Talk about the things that must be paid attention to when interviewing programmers
- Summary and practice of knowledge map construction technology
- Interpretation of mask RCNN paper
- Win: add general users to the local admins group
猜你喜欢
如何搭建一支搞垮公司的技术团队?
[uc/os-iii] chapter 1.2.3.4 understanding RTOS
Win:使用 Shadow Mode 查看远程用户的桌面会话
Stored procedure and stored function in Oracle
[download white paper] does your customer relationship management (CRM) really "manage" customers?
Asynchronous and promise
Grub 2.12 will be released this year to continue to improve boot security
Practical case of SQL optimization: speed up your database
Interesting practice of robot programming 16 synchronous positioning and map building (SLAM)
[source code attached] Intelligent Recommendation System Based on knowledge map -sylvie rabbit
随机推荐
Leetcode takes out the least number of magic beans
"C zero foundation introduction hundred knowledge and hundred cases" (72) multi wave entrustment -- Mom shouted for dinner
Practice of tdengine in TCL air conditioning energy management platform
[机缘参悟-38]:鬼谷子-第五飞箝篇 - 警示之一:有一种杀称为“捧杀”
WCF: expose unset read-only DataMember property- WCF: Exposing readonly DataMember properties without set?
The application and Optimization Practice of redis in vivo push platform is transferred to the end of metadata by
Advanced learning of MySQL -- Application -- Introduction
力扣剑指offer——二叉树篇
Win:使用 PowerShell 检查无线信号的强弱
Learn game model 3D characters, come out to find a job?
PowerShell: use PowerShell behind the proxy server
Matrixone 0.2.0 is released, and the fastest SQL computing engine is coming
Action News
Limited query of common SQL operations
One plus six brushes into Kali nethunter
How to make a cool ink screen electronic clock?
Binary tree traversal - middle order traversal (golang)
R language uses logistic regression and afrima, ARIMA time series models to predict world population
Outlook:总是提示输入用户密码
Interesting practice of robot programming 14 robot 3D simulation (gazebo+turtlebot3)