当前位置:网站首页>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
}
边栏推荐
- Slam learning notes - build a complete gazebo multi machine simulation slam from scratch (III)
- Microservice - declarative interface call openfeign
- Brush questions -- sword finger offer
- 远程文件包含实操
- 架构实战营 - 第 6 期 毕业总结
- "Remake Apple product UI with Android" (2) -- silky Appstore card transition animation
- "Remake Apple product UI with Android" (3) - elegant statistical chart
- “用Android复刻Apple产品UI”(3)—优雅的数据统计图表
- Effect of ARP package on FTP dump under vxworks-6.6 system
- [proteus simulation] 8 × 8LED dot matrix screen imitates elevator digital scrolling display
猜你喜欢
[系统安全] 四十三.Powershell恶意代码检测系列 (5)抽象语法树自动提取万字详解
Getting started with Message Oriented Middleware
Distributed task scheduling XXL job
Microservice - Nacos registration center and configuration center
Deep understanding of grouping sets statements in SQL
[200 opencv routines] 217 Mouse interaction to obtain polygon area (ROI)
请做好3年内随时失业的准备?
“用Android复刻Apple产品UI”(3)—优雅的数据统计图表
App mobile terminal test [4] APK operation
App mobile terminal test [3] ADB command
随机推荐
0214-27100 a day with little fluctuation
【Proteus仿真】8×8LED点阵屏仿电梯数字滚动显示
Is it safe to open an account with flush?
[combinatorics] non descending path problem (outline of non descending path problem | basic model of non descending path problem | non descending path problem expansion model 1 non origin starting poi
Reflection on some things
疫情常态化大背景下,关于远程办公的思考|社区征文
Is it safe to open an account with tongdaxin?
Cocos Creator 2.x 自动打包(构建 + 编译)
初试scikit-learn库
nifi从入门到实战(保姆级教程)——flow
[combinatorics] summary of combinatorial identities (eleven combinatorial identities | proof methods of combinatorial identities | summation methods)*
Slam learning notes - build a complete gazebo multi machine simulation slam from scratch (4)
《天天数学》连载56:二月二十五日
请做好3年内随时失业的准备?
Please be prepared to lose your job at any time within 3 years?
记一次jar包冲突解决过程
Chinese translation of Tagore's floating birds (1~10)
Getting started with Message Oriented Middleware
Approval process design
阿飞的期望