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

边栏推荐
- Do you know the eight signs of a team becoming agile?
- Asynchronous and promise
- Restful fast request 2022.2.1 release, support curl import
- spoon插入更新oracle数据库,插了一部分提示报错Assertion botch: negative time
- The most powerful new household god card of Bank of communications. Apply to earn 2100 yuan. Hurry up if you haven't applied!
- Rabbit MQ message sending of vertx
- Process scheduling and termination
- Kotlin - coroutine
- CAM Pytorch
- Use the difference between "Chmod a + X" and "Chmod 755" [closed] - difference between using "Chmod a + X" and "Chmod 755" [closed]
猜你喜欢

Visual studio 2019 set transparent background (fool teaching)

力扣剑指offer——二叉树篇

Matrixone 0.2.0 is released, and the fastest SQL computing engine is coming

runc hang 导致 Kubernetes 节点 NotReady

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

Chinese natural language processing, medical, legal and other public data sets, sorting and sharing

如何搭建一支搞垮公司的技术团队?

A label colorful navigation bar

Action News

The perfect car for successful people: BMW X7! Superior performance, excellent comfort and safety
随机推荐
Win:将一般用户添加到 Local Admins 组中
RichView TRVUnits 图像显示单位
Numpy library introductory tutorial: basic knowledge summary
Timescaledb 2.5.2 release, time series database based on PostgreSQL
Prometheus monitors the correct posture of redis cluster
Visual explanation of Newton iteration method
Outlook:总是提示输入用户密码
CAM Pytorch
Android advanced interview question record in 2022
Openresty ngx Lua Execution stage
Word processing software
Codeforces Round #770 (Div. 2) ABC
The steering wheel can be turned for one and a half turns. Is there any difference between it and two turns
Leetcode takes out the least number of magic beans
Richview trvunits image display units
Win: use shadow mode to view the Desktop Session of a remote user
力扣剑指offer——二叉树篇
What is the length of SHA512 hash string- What is the length of a hashed string with SHA512?
Bert fine tuning skills experiment
Talk about the things that must be paid attention to when interviewing programmers