mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-08 08:50:15 +08:00
update 0450.删除二叉搜索树中的节点: 修改错字,优化 go 代码风格
This commit is contained in:
@ -25,7 +25,7 @@
|
|||||||
|
|
||||||
# 思路
|
# 思路
|
||||||
|
|
||||||
搜索树的节点删除要比节点增加复杂的多,有很多情况需要考虑,做好心里准备。
|
搜索树的节点删除要比节点增加复杂的多,有很多情况需要考虑,做好心理准备。
|
||||||
|
|
||||||
## 递归
|
## 递归
|
||||||
|
|
||||||
@ -33,7 +33,7 @@
|
|||||||
|
|
||||||
* 确定递归函数参数以及返回值
|
* 确定递归函数参数以及返回值
|
||||||
|
|
||||||
说道递归函数的返回值,在[二叉树:搜索树中的插入操作](https://programmercarl.com/0701.二叉搜索树中的插入操作.html)中通过递归返回值来加入新节点, 这里也可以通过递归返回值删除节点。
|
说到递归函数的返回值,在[二叉树:搜索树中的插入操作](https://programmercarl.com/0701.二叉搜索树中的插入操作.html)中通过递归返回值来加入新节点, 这里也可以通过递归返回值删除节点。
|
||||||
|
|
||||||
代码如下:
|
代码如下:
|
||||||
|
|
||||||
@ -66,7 +66,7 @@ if (root == nullptr) return root;
|
|||||||
|
|
||||||

|

|
||||||
|
|
||||||
动画中棵二叉搜索树中,删除元素7, 那么删除节点(元素7)的左孩子就是5,删除节点(元素7)的右子树的最左面节点是元素8。
|
动画中的二叉搜索树中,删除元素7, 那么删除节点(元素7)的左孩子就是5,删除节点(元素7)的右子树的最左面节点是元素8。
|
||||||
|
|
||||||
将删除节点(元素7)的左孩子放到删除节点(元素7)的右子树的最左面节点(元素8)的左孩子上,就是把5为根节点的子树移到了8的左孩子的位置。
|
将删除节点(元素7)的左孩子放到删除节点(元素7)的右子树的最左面节点(元素8)的左孩子上,就是把5为根节点的子树移到了8的左孩子的位置。
|
||||||
|
|
||||||
@ -251,7 +251,7 @@ public:
|
|||||||
|
|
||||||
**这里最关键的逻辑就是第五种情况(删除一个左右孩子都不为空的节点),这种情况一定要想清楚**。
|
**这里最关键的逻辑就是第五种情况(删除一个左右孩子都不为空的节点),这种情况一定要想清楚**。
|
||||||
|
|
||||||
而且就算想清楚了,对应的代码也未必可以写出来,所以**这道题目即考察思维逻辑,也考察代码能力**。
|
而且就算想清楚了,对应的代码也未必可以写出来,所以**这道题目既考察思维逻辑,也考察代码能力**。
|
||||||
|
|
||||||
递归中我给出了两种写法,推荐大家学会第一种(利用搜索树的特性)就可以了,第二种递归写法其实是比较绕的。
|
递归中我给出了两种写法,推荐大家学会第一种(利用搜索树的特性)就可以了,第二种递归写法其实是比较绕的。
|
||||||
|
|
||||||
@ -390,39 +390,39 @@ class Solution:
|
|||||||
```Go
|
```Go
|
||||||
// 递归版本
|
// 递归版本
|
||||||
func deleteNode(root *TreeNode, key int) *TreeNode {
|
func deleteNode(root *TreeNode, key int) *TreeNode {
|
||||||
if root==nil{
|
if root == nil {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
if key<root.Val{
|
if key < root.Val {
|
||||||
root.Left=deleteNode(root.Left,key)
|
root.Left = deleteNode(root.Left, key)
|
||||||
return root
|
return root
|
||||||
}
|
}
|
||||||
if key>root.Val{
|
if key > root.Val {
|
||||||
root.Right=deleteNode(root.Right,key)
|
root.Right = deleteNode(root.Right, key)
|
||||||
return root
|
return root
|
||||||
}
|
}
|
||||||
if root.Right==nil{
|
if root.Right == nil {
|
||||||
return root.Left
|
return root.Left
|
||||||
}
|
}
|
||||||
if root.Left==nil{
|
if root.Left == nil{
|
||||||
return root.Right
|
return root.Right
|
||||||
}
|
}
|
||||||
minnode:=root.Right
|
minnode := root.Right
|
||||||
for minnode.Left!=nil{
|
for minnode.Left != nil {
|
||||||
minnode=minnode.Left
|
minnode = minnode.Left
|
||||||
}
|
}
|
||||||
root.Val=minnode.Val
|
root.Val = minnode.Val
|
||||||
root.Right=deleteNode1(root.Right)
|
root.Right = deleteNode1(root.Right)
|
||||||
return root
|
return root
|
||||||
}
|
}
|
||||||
|
|
||||||
func deleteNode1(root *TreeNode)*TreeNode{
|
func deleteNode1(root *TreeNode)*TreeNode {
|
||||||
if root.Left==nil{
|
if root.Left == nil {
|
||||||
pRight:=root.Right
|
pRight := root.Right
|
||||||
root.Right=nil
|
root.Right = nil
|
||||||
return pRight
|
return pRight
|
||||||
}
|
}
|
||||||
root.Left=deleteNode1(root.Left)
|
root.Left = deleteNode1(root.Left)
|
||||||
return root
|
return root
|
||||||
}
|
}
|
||||||
// 迭代版本
|
// 迭代版本
|
||||||
|
Reference in New Issue
Block a user