当前位置:网站首页>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
}
边栏推荐
- Page dynamics [2]keyframes
- Slam learning notes - build a complete gazebo multi machine simulation slam from scratch (III)
- Pandora IOT development board learning (HAL Library) - Experiment 5 external interrupt experiment (learning notes)
- [combinatorics] combinatorial identities (review of eight combinatorial identities | product of combinatorial identities 1 | proof | use scenario | general method for finding combinatorial numbers)
- Slam learning notes - build a complete gazebo multi machine simulation slam from scratch (II)
- [web security] - [SQL injection] - error detection injection
- [combinatorics] summary of combinatorial identities (eleven combinatorial identities | proof methods of combinatorial identities | summation methods)*
- 面试官:JVM如何分配和回收堆外内存
- Q2 encryption market investment and financing report in 2022: gamefi becomes an investment keyword
- ASEMI整流桥UMB10F参数,UMB10F规格,UMB10F封装
猜你喜欢

Microservice API gateway zuul

First knowledge of database

Getting started with Message Oriented Middleware

“用Android复刻Apple产品UI”(3)—优雅的数据统计图表

记一次jar包冲突解决过程

关于网页中的文本选择以及统计选中文本长度

请做好3年内随时失业的准备?

Microservices Seata distributed transactions

Semi supervised learning

The accept attribute of the El upload upload component restricts the file type (detailed explanation of the case)
随机推荐
Multithread 02 thread join
Rk3399 platform development series explanation (WiFi) 5.54. What is WiFi wireless LAN
坚持输出需要不断学习
Cocos Creator 2.x 自动打包(构建 + 编译)
NSQ源码安装运行过程
Q2 encryption market investment and financing report in 2022: gamefi becomes an investment keyword
EditText request focus - EditText request focus
Detailed explanation of four modes of distributed transaction (Seata)
拼夕夕二面:说说布隆过滤器与布谷鸟过滤器?应用场景?我懵了。。
PHP CI(CodeIgniter)log级别设置
Redis high availability and persistence
嵌入式开发:避免开源软件的7个理由
PHP二级域名session共享方案
用通达信炒股开户安全吗?
Break through 1million, sword finger 2million!
程序猿如何快速成长
How to thicken the brush in the graphical interface
Advanced Mathematics (Seventh Edition) Tongji University exercises 2-1 personal solutions
首发!!lancet饿了么官方文档
Pyinstaller is not an internal or external command, nor is it a runnable program or batch file