mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-08 16:54:50 +08:00
Merge pull request #60 from QuinnDK/添加0450删除二叉搜索树中的节点
添加0450删除二叉搜索树中的节点 Go版本
This commit is contained in:
@ -257,6 +257,44 @@ Python:
|
|||||||
|
|
||||||
|
|
||||||
Go:
|
Go:
|
||||||
|
```Go
|
||||||
|
func deleteNode(root *TreeNode, key int) *TreeNode {
|
||||||
|
if root==nil{
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
if key<root.Val{
|
||||||
|
root.Left=deleteNode(root.Left,key)
|
||||||
|
return root
|
||||||
|
}
|
||||||
|
if key>root.Val{
|
||||||
|
root.Right=deleteNode(root.Right,key)
|
||||||
|
return root
|
||||||
|
}
|
||||||
|
if root.Right==nil{
|
||||||
|
return root.Left
|
||||||
|
}
|
||||||
|
if root.Left==nil{
|
||||||
|
return root.Right
|
||||||
|
}
|
||||||
|
minnode:=root.Right
|
||||||
|
for minnode.Left!=nil{
|
||||||
|
minnode=minnode.Left
|
||||||
|
}
|
||||||
|
root.Val=minnode.Val
|
||||||
|
root.Right=deleteNode1(root.Right)
|
||||||
|
return root
|
||||||
|
}
|
||||||
|
|
||||||
|
func deleteNode1(root *TreeNode)*TreeNode{
|
||||||
|
if root.Left==nil{
|
||||||
|
pRight:=root.Right
|
||||||
|
root.Right=nil
|
||||||
|
return pRight
|
||||||
|
}
|
||||||
|
root.Left=deleteNode1(root.Left)
|
||||||
|
return root
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user