Merge pull request #60 from QuinnDK/添加0450删除二叉搜索树中的节点

添加0450删除二叉搜索树中的节点 Go版本
This commit is contained in:
Carl Sun 2021-05-13 17:19:44 +08:00 committed by GitHub
commit 3f5e7f1696
1 changed files with 38 additions and 0 deletions

View File

@ -257,6 +257,44 @@ Python
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
}
```