当前位置:网站首页>【LeetCode】98. Verify the binary search tree (2 brushes of wrong questions)
【LeetCode】98. Verify the binary search tree (2 brushes of wrong questions)
2022-07-05 02:20:00 【Kaimar】
- Ideas
The middle order traversal of binary search tree is a monotonically increasing sequence , Therefore, based on this, we can use the results of the middle order traversal to judge .
var prev *TreeNode = nil // Previous node
func isValidBST(root *TreeNode) bool {
if root == nil {
return true
}
// Left
left := isValidBST(root.Left)
// in
if prev != nil && prev.Val >= root.Val {
return false
}
prev = root
// Right
right := isValidBST(root.Right)
return left && right
}
Encounter a pit , I can live alone , It's about global variables , Avoid using global variables .
/** * Definition for a binary tree node. * type TreeNode struct { * Val int * Left *TreeNode * Right *TreeNode * } */
func isValidBST(root *TreeNode) bool {
var preNode *TreeNode
var valid func(node *TreeNode) bool
valid = func(node *TreeNode) bool {
if node == nil {
return true
}
// Left
left := valid(node.Left)
// in
if preNode != nil && preNode.Val >= node.Val {
return false
}
preNode = node
// Right
right := valid(node.Right)
return left && right
}
return valid(root)
}
边栏推荐
- Win:使用 PowerShell 检查无线信号的强弱
- [technology development-26]: data security of new information and communication networks
- Why do you understand a16z? Those who prefer Web3.0 Privacy Infrastructure: nym
- Tla+ through examples (XI) -- propositional logic and examples
- 如何搭建一支搞垮公司的技術團隊?
- Exploration of short text analysis in the field of medical and health (I)
- R language uses logistic regression and afrima, ARIMA time series models to predict world population
- Kotlin - coroutine
- openresty ngx_lua執行階段
- Valentine's Day flirting with girls to force a small way, one can learn
猜你喜欢
Open source SPL optimized report application coping endlessly
Win: use shadow mode to view the Desktop Session of a remote user
A tab Sina navigation bar
Yolov5 model training and detection
Interesting practice of robot programming 14 robot 3D simulation (gazebo+turtlebot3)
Exploration of short text analysis in the field of medical and health (I)
Restful Fast Request 2022.2.1发布,支持cURL导入
Traditional chips and AI chips
【附源码】基于知识图谱的智能推荐系统-Sylvie小兔
The perfect car for successful people: BMW X7! Superior performance, excellent comfort and safety
随机推荐
Yyds dry inventory jetpack hit dependency injection framework Getting Started Guide
A label making navigation bar
Exploration of short text analysis in the field of medical and health (I)
Why do you understand a16z? Those who prefer Web3.0 Privacy Infrastructure: nym
Word processing software
A tab Sina navigation bar
Icu4c 70 source code download and compilation (win10, vs2022)
Redis' hyperloglog as a powerful tool for active user statistics
丸子百度小程序详细配置教程,审核通过。
openresty ngx_lua执行阶段
Win:使用组策略启用和禁用 USB 驱动器
What is the length of SHA512 hash string- What is the length of a hashed string with SHA512?
如何搭建一支搞垮公司的技术团队?
Can you really learn 3DMAX modeling by self-study?
Binary tree traversal - middle order traversal (golang)
Missile interception -- UPC winter vacation training match
[illumination du destin - 38]: Ghost Valley - chapitre 5 Flying clamp - one of the Warnings: There is a kind of killing called "hold Kill"
A label colorful navigation bar
Variables in postman
Win: enable and disable USB drives using group policy