Skip to content

Commit b80ff0b

Browse files
committed
feat: Solution for Problem #450 - Delete Node in a BST
1 parent bf046a2 commit b80ff0b

File tree

2 files changed

+125
-0
lines changed

2 files changed

+125
-0
lines changed
Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
/*:
2+
# 450. Delete Node in a BST [Medium]
3+
https://leetcode.com/problems/delete-node-in-a-bst/
4+
5+
---
6+
7+
### Problem Statement:
8+
9+
Given a root node reference of a BST and a key, delete the node with the given key in the BST. Return the root node reference (possibly updated) of the BST.
10+
11+
Basically, the deletion can be divided into two stages:
12+
13+
+ Search for a node to remove.
14+
+ If the node is found, delete the node.
15+
16+
### Example:
17+
18+
```
19+
root = [5,3,6,2,4,null,7]
20+
key = 3
21+
22+
5
23+
/ \
24+
3 6
25+
/ \ \
26+
2 4 7
27+
28+
Given key to delete is 3. So we find the node with value 3 and delete it.
29+
30+
One valid answer is [5,4,6,2,null,null,7], shown in the following BST.
31+
32+
5
33+
/ \
34+
4 6
35+
/ \
36+
2 7
37+
38+
Another valid answer is [5,2,6,null,4,null,7].
39+
40+
5
41+
/ \
42+
2 6
43+
\ \
44+
4 7
45+
46+
```
47+
48+
### Notes:
49+
+ Time complexity should be O(height of tree).
50+
51+
*/
52+
53+
54+
import UIKit
55+
56+
// Definition for a binary tree node.
57+
public class TreeNode {
58+
public var val: Int
59+
public var left: TreeNode?
60+
public var right: TreeNode?
61+
public init() { self.val = 0; self.left = nil; self.right = nil; }
62+
public init(_ val: Int) { self.val = val; self.left = nil; self.right = nil; }
63+
public init(_ val: Int, _ left: TreeNode?, _ right: TreeNode?) {
64+
self.val = val
65+
self.left = left
66+
self.right = right
67+
}
68+
}
69+
70+
// 85 / 85 test cases passed.
71+
// Status: Accepted
72+
// Runtime: 80 ms
73+
// Memory Usage: 22.4 MB
74+
75+
class Solution {
76+
func deleteNode(_ root: TreeNode?, _ key: Int) -> TreeNode? {
77+
78+
guard let root = root else {
79+
return nil
80+
}
81+
82+
if root.val > key {
83+
root.left = deleteNode(root.left, key)
84+
} else if root.val < key {
85+
root.right = deleteNode(root.right, key)
86+
} else {
87+
if root.left == nil {
88+
return root.right
89+
} else if root.right == nil {
90+
return root.left
91+
} else {
92+
let minNode = findMin(root.right!)
93+
root.val = minNode.val
94+
root.right = deleteNode(root.right, root.val)
95+
}
96+
}
97+
98+
return root
99+
}
100+
101+
func findMin(_ root: TreeNode) -> TreeNode {
102+
var root = root
103+
104+
while let leftNode = root.left {
105+
root = leftNode
106+
}
107+
108+
return root
109+
}
110+
}
111+
112+
let node = TreeNode(5)
113+
node.left = TreeNode(3)
114+
node.right = TreeNode(6)
115+
node.left?.left = TreeNode(2)
116+
node.left?.right = TreeNode(4)
117+
node.right?.right = TreeNode(7)
118+
119+
120+
let sol = Solution()
121+
sol.deleteNode(node, 3)
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
2+
<playground version='5.0' target-platform='ios'>
3+
<timeline fileName='timeline.xctimeline'/>
4+
</playground>

0 commit comments

Comments
 (0)