当前位置:网站首页>Leetcode binary search tree
Leetcode binary search tree
2022-07-03 16:16:00 【Dawnlighttt】
List of articles
q98 Verify binary search tree
Answer key
Use preorder to traverse , Use dfs To traverse , The binary sort tree should satisfy that all nodes of the left subtree are smaller than the root node , All nodes of the right subtree are larger than the root node . Therefore, a maximum value and a minimum value are maintained in the parameter list of recursive functions , In left recursion , Update the maximum value to the current node , Because there should be no number larger than this maximum in the left subtree ; At the same time, when right recursion , Update the minimum value to the current node , Because there should be no number smaller than this minimum in the right subtree .
func isValidBST(root *TreeNode) bool {
return dfs(root, math.MinInt64, math.MaxInt64)
}
func dfs(root *TreeNode, min, max int) bool {
if root == nil {
return true
}
// Determine whether it is within this range
if root.Val <= min || root.Val >= max {
return false
}
// The maximum value of update left recursion is the current node
// The minimum value of update right recursion is the current node
return dfs(root.Left, min, root.Val) && dfs(root.Right, root.Val, max)
}
q450 Delete nodes in binary search tree
Answer key
Delete BST The nodes in the , There are three situations :
1 If key < root.Val, The node to be deleted is BST The left subtree , Then recursively delete the left subtree
2 If key > root.Val, The node to be deleted is BST The right subtree , Then recursively delete the right subtree
3 If key = root.Val, Note that the node to be deleted is this node , This category is divided into the following three cases :
- No right or left subtree , Just delete it
- There are right subtrees , Replace the current node with the subsequent node , Then recursively delete the subsequent node in the right subtree
- There's a Zuozi tree , Replace the current node with the forward node , Then recursively delete the forward node in the left subtree
The way to find the forward node , It is to recursively traverse its right node in the left subtree of the current node , Until the right node is empty , The current node is the forward node .
The way to find the successor node is to recursively traverse its left node in the right subtree of the current node , Until the left node is empty , The current node is the successor node .
func deleteNode(root *TreeNode, key int) *TreeNode {
if root == nil {
return nil
// Go to the left tree
} else if key < root.Val {
root.Left = deleteNode(root.Left, key)
// Go to the right subtree
} else if key > root.Val {
root.Right = deleteNode(root.Right, key)
// The value of the current node is key
} else {
// No right or left subtree , Delete directly
if root.Left == nil && root.Right == nil {
root = nil
// There are right subtrees , Change the successor node to the current position , Then delete the successor node
} else if root.Right != nil {
// Replace
root.Val = Successor(root).Val
// Delete subsequent nodes
root.Right = deleteNode(root.Right, root.Val)
// There's a Zuozi tree , Change the forward node to the current position , Then delete the forward node
} else {
root.Val = Predecessor(root).Val
root.Left = deleteNode(root.Left, root.Val)
}
}
return root
}
// Predecessor Looking for forward nodes
func Predecessor(node *TreeNode) *TreeNode {
pre := node.Left
for pre.Right != nil {
pre = pre.Right
}
return pre
}
// Successor Looking for successor nodes
func Successor(node *TreeNode) *TreeNode {
post := node.Right
for post.Left != nil {
post = post.Left
}
return post
}
q701 Insert operation in binary search tree
Answer key
Compare with the current node when inserting , If it is smaller than the current node, continue to traverse and insert it into the left subtree , If it is larger than the current node, continue to traverse and insert it into the right subtree .
func insertIntoBST(root *TreeNode, val int) *TreeNode {
if root == nil {
return &TreeNode{
Val: val}
}
p := root
for p != nil {
if val < p.Val {
if p.Left == nil {
p.Left = &TreeNode{
Val: val}
break
}
p = p.Left
} else {
if p.Right == nil {
p.Right = &TreeNode{
Val: val}
break
}
p = p.Right
}
}
return root
}
边栏推荐
- 切入点表达式
- 在ntpdate同步时间的时候出现“the NTP socket is in use, exiting”
- Slam learning notes - build a complete gazebo multi machine simulation slam from scratch (4)
- SVN使用规范
- 探索Cassandra的去中心化分布式架构
- Microservice - declarative interface call openfeign
- 初试scikit-learn库
- Détails du contrôle de la congestion TCP | 3. Espace de conception
- Shell script import and export data
- 请求头不同国家和语言的表示
猜你喜欢

Détails du contrôle de la congestion TCP | 3. Espace de conception

Brush questions -- sword finger offer
![[proteus simulation] 8 × 8LED dot matrix screen imitates elevator digital scrolling display](/img/46/c7f566f8fd46d383b055582d680bb7.png)
[proteus simulation] 8 × 8LED dot matrix screen imitates elevator digital scrolling display

ThreeJS 第二篇:顶点概念、几何体结构

拼夕夕二面:说说布隆过滤器与布谷鸟过滤器?应用场景?我懵了。。

【Proteus仿真】74HC595+74LS154驱动显示16X16点阵

The difference between calling by value and simulating calling by reference

深入理解 SQL 中的 Grouping Sets 语句

“用Android复刻Apple产品UI”(2)——丝滑的AppStore卡片转场动画

Record a jar package conflict resolution process
随机推荐
Low level version of drawing interface (explain each step in detail)
切入点表达式
关于网页中的文本选择以及统计选中文本长度
Using optimistic lock and pessimistic lock in MySQL to realize distributed lock
TCP拥塞控制详解 | 3. 设计空间
Client does not support authentication protocol requested by server; consider upgrading MySQL client
Unity project optimization case 1
远程办公之大家一同实现合作编辑资料和开发文档 | 社区征文
Nifi from introduction to practice (nanny level tutorial) - flow
The accept attribute of the El upload upload component restricts the file type (detailed explanation of the case)
App mobile terminal test [4] APK operation
Unity项目优化案例一
Go language self-study series | if else if statement in golang
pyinstaller不是内部或外部命令,也不是可运行的程序 或批处理文件
Pychart error updating package list: connect timed out
Qt插件之自定义插件构建和使用
Pandora IOT development board learning (HAL Library) - Experiment 5 external interrupt experiment (learning notes)
Break through 1million, sword finger 2million!
Colab works with Google cloud disk
Mb10m-asemi rectifier bridge mb10m