diff --git a/leetcode/leetcode75/BinarySearchTree/450.DeleteNodeInaBST/deleteNodeInaBST.go b/leetcode/leetcode75/BinarySearchTree/450.DeleteNodeInaBST/deleteNodeInaBST.go new file mode 100644 index 0000000..21743ca --- /dev/null +++ b/leetcode/leetcode75/BinarySearchTree/450.DeleteNodeInaBST/deleteNodeInaBST.go @@ -0,0 +1,45 @@ +package main + +/** + * Definition for a binary tree node. + * type TreeNode struct { + * Val int + * Left *TreeNode + * Right *TreeNode + * } + */ + +type TreeNode struct { + Val int + Left *TreeNode + Right *TreeNode +} + +func deleteNode(root *TreeNode, key int) *TreeNode { + if root == nil { + return nil + } + if root.Val > key { + root.Left = deleteNode(root.Left, key) + return root + } + if root.Val < key { + root.Right = deleteNode(root.Right, key) + return root + } + if root.Left == nil { + return root.Right + } + if root.Right == nil { + return root.Left + } + node := root.Right + for node.Left != nil { + node = node.Left + } + node.Left = root.Left + root = root.Right + return root +} + +func main() {}