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

边栏推荐
- CAM Pytorch
- Win: use shadow mode to view the Desktop Session of a remote user
- Win:使用 PowerShell 检查无线信号的强弱
- [source code attached] Intelligent Recommendation System Based on knowledge map -sylvie rabbit
- Use the difference between "Chmod a + X" and "Chmod 755" [closed] - difference between using "Chmod a + X" and "Chmod 755" [closed]
- Application and Optimization Practice of redis in vivo push platform
- Open source SPL optimized report application coping endlessly
- 172. Zero after factorial
- Action News
- Binary tree traversal - middle order traversal (golang)
猜你喜欢
![[技术发展-26]:新型信息与通信网络的数据安全](/img/13/10c8bd340017c6516edef41cd3bf6f.png)
[技术发展-26]:新型信息与通信网络的数据安全

Icu4c 70 source code download and compilation (win10, vs2022)

Li Kou Jianzhi offer -- binary tree chapter

Subject 3 how to turn on the high beam diagram? Is the high beam of section 3 up or down

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

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

PowerShell:在代理服务器后面使用 PowerShell

Yolov5 model training and detection

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

Learn game model 3D characters, come out to find a job?
随机推荐
Grpc message sending of vertx
Interesting practice of robot programming 15- autoavoidobstacles
使用druid連接MySQL數據庫報類型錯誤
Which common ports should the server open
WCF: expose unset read-only DataMember property- WCF: Exposing readonly DataMember properties without set?
Use the difference between "Chmod a + X" and "Chmod 755" [closed] - difference between using "Chmod a + X" and "Chmod 755" [closed]
100 basic multiple choice questions of C language (with answers) 04
[技术发展-26]:新型信息与通信网络的数据安全
I use these six code comparison tools
Serious bugs with lifted/nullable conversions from int, allowing conversion from decimal
openresty ngx_lua执行阶段
【附源码】基于知识图谱的智能推荐系统-Sylvie小兔
Learn tla+ (XII) -- functions through examples
Some query constructors in laravel (2)
RichView TRVUnits 图像显示单位
How to build a technical team that will bring down the company?
Tla+ through examples (XI) -- propositional logic and examples
Do you know the eight signs of a team becoming agile?
Official announcement! The third cloud native programming challenge is officially launched!
The MySQL team development specifications used by various factories are too detailed. It is recommended to collect them!