当前位置:网站首页>【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)
}
边栏推荐
- Redis distributed lock, lock code logic
- Pytorch register_ Hook (operate on gradient grad)
- Pytorch fine tuning (Fortune): hollowed out design or cheating
- R language uses logistic regression and afrima, ARIMA time series models to predict world population
- Abacus mental arithmetic test
- spoon插入更新oracle数据库,插了一部分提示报错Assertion botch: negative time
- RichView TRVUnits 图像显示单位
- When to catch an exception and when to throw an exception- When to catch the Exception vs When to throw the Exceptions?
- Win:使用 PowerShell 检查无线信号的强弱
- The MySQL team development specifications used by various factories are too detailed. It is recommended to collect them!
猜你喜欢
Naacl 2021 | contrastive learning sweeping text clustering task
Write a thread pool by hand, and take you to learn the implementation principle of ThreadPoolExecutor thread pool
Restful fast request 2022.2.1 release, support curl import
openresty ngx_lua執行階段
Visual explanation of Newton iteration method
Yolov5 model training and detection
Prometheus monitors the correct posture of redis cluster
JVM - when multiple threads initialize the same class, only one thread is allowed to initialize
"C zero foundation introduction hundred knowledge and hundred cases" (72) multi wave entrustment -- Mom shouted for dinner
Matrixone 0.2.0 is released, and the fastest SQL computing engine is coming
随机推荐
ICSI 311 Parser
Pytorch fine tuning (Fortune): hollowed out design or cheating
Serious bugs with lifted/nullable conversions from int, allowing conversion from decimal
Richview trvunits image display units
[Yu Yue education] National Open University spring 2019 0505-22t basic nursing reference questions
Numpy library introductory tutorial: basic knowledge summary
LeetCode 314. Binary tree vertical order traversal - Binary Tree Series Question 6
官宣!第三届云原生编程挑战赛正式启动!
[機緣參悟-38]:鬼穀子-第五飛箝篇 - 警示之一:有一種殺稱為“捧殺”
使用druid連接MySQL數據庫報類型錯誤
Prometheus monitors the correct posture of redis cluster
Advanced conditional statements of common SQL operations
[Digital IC hand tearing code] Verilog edge detection circuit (rising edge, falling edge, double edge) | topic | principle | design | simulation
Missile interception -- UPC winter vacation training match
[understanding of opportunity -38]: Guiguzi - Chapter 5 flying clamp - warning one: there is a kind of killing called "killing"
Learn game model 3D characters, come out to find a job?
使用druid连接MySQL数据库报类型错误
openresty ngx_lua变量操作
Write a thread pool by hand, and take you to learn the implementation principle of ThreadPoolExecutor thread pool
Application and Optimization Practice of redis in vivo push platform