当前位置:网站首页>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
}
边栏推荐
- 特征多项式与常系数齐次线性递推
- Golang 装饰器模式以及在NSQ中的使用
- Microservice API gateway zuul
- [combinatorics] summary of combinatorial identities (eleven combinatorial identities | proof methods of combinatorial identities | summation methods)*
- 深入理解 SQL 中的 Grouping Sets 语句
- First knowledge of database
- 请求头不同国家和语言的表示
- The accept attribute of the El upload upload component restricts the file type (detailed explanation of the case)
- Redis高可用与持久化
- 0214-27100 a day with little fluctuation
猜你喜欢
The difference between calling by value and simulating calling by reference
深入理解 SQL 中的 Grouping Sets 语句
嵌入式开发:避免开源软件的7个理由
【OpenCV 例程200篇】217. 鼠标交互获取多边形区域(ROI)
Distributed task scheduling XXL job
Asemi rectifier bridge umb10f parameters, umb10f specifications, umb10f package
Mixlab编辑团队招募队友啦~~
Embedded development: seven reasons to avoid open source software
Microservice sentinel flow control degradation
Famous blackmail software stops operation and releases decryption keys. Most hospital IOT devices have security vulnerabilities | global network security hotspot on February 14
随机推荐
《天天数学》连载56:二月二十五日
Uploads labs range (with source code analysis) (under update)
[proteus simulation] 8 × 8LED dot matrix screen imitates elevator digital scrolling display
Go语言自学系列 | golang中的if else if语句
Batch files: list all files in a directory with relative paths - batch files: list all files in a directory with relative paths
Using optimistic lock and pessimistic lock in MySQL to realize distributed lock
Golang 匿名函数使用
【Proteus仿真】8×8LED点阵屏仿电梯数字滚动显示
PHP二级域名session共享方案
Explore Cassandra's decentralized distributed architecture
架构实战营 - 第 6 期 毕业总结
How idea starts run dashboard
几种常见IO模型的原理
Getting started with Message Oriented Middleware
[200 opencv routines] 217 Mouse interaction to obtain polygon area (ROI)
Semi supervised learning
【OpenCV 例程200篇】217. 鼠标交互获取多边形区域(ROI)
[proteus simulation] 74hc595+74ls154 drive display 16x16 dot matrix
nifi从入门到实战(保姆级教程)——flow
[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