-
Notifications
You must be signed in to change notification settings - Fork 1
/
leetcode-450-!!!-DeleteNodeInABst.js
97 lines (83 loc) · 3.02 KB
/
leetcode-450-!!!-DeleteNodeInABst.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
// key = 8 16
// / \
// ----> 8 24
// / \ / \
// 4 12 20 28
// / \ / \ / \ / \
// 2 6 10 14 18 22 26 30
// / \ / \ / \ / \
// 1 3 5 7 (9) 11 13 15
// get 9 - the smallest on 8's right
//
//
// 16
// / \
// (9) 24
// / \ / \
// 4 12 20 28
// / \ / \ / \ / \
// 2 6 10 14 18 22 26 30
// / \ / \ \ / \
// 1 3 5 7 11 13 15
function TreeNode(val) {
this.val = val;
this.left = this.right = null;
}
function deleteNode(root, key) {
if (root === null) {
return null; // Node not found
}
if (key < root.val) {
// If key is smaller, it's in the left subtree
root.left = deleteNode(root.left, key);
} else if (key > root.val) {
// If key is larger, it's in the right subtree
root.right = deleteNode(root.right, key);
} else {
// Node with the key found, delete it
// Case 1: Node with only one child or no child
if (root.left === null) {
return root.right;
} else if (root.right === null) {
return root.left;
}
// Case 2: Node with two children
// Find the smallest node in the right subtree (inorder successor)
root.val = findMin(root.right).val;
// Delete the inorder successor
root.right = deleteNode(root.right, root.val);
}
return root;
}
function findMin(node) {
while (node.left !== null) {
node = node.left;
}
return node;
}
// Example usage:
const root1 = new TreeNode(5);
root1.left = new TreeNode(3);
root1.right = new TreeNode(6);
root1.left.left = new TreeNode(2);
root1.left.right = new TreeNode(4);
root1.right.right = new TreeNode(7);
console.log("Input:", JSON.stringify(root1));
const key1 = 3;
const updatedRoot1 = deleteNode(root1, key1);
console.log("Output:", JSON.stringify(updatedRoot1));
const root2 = new TreeNode(5);
root2.left = new TreeNode(3);
root2.right = new TreeNode(6);
root2.left.left = new TreeNode(2);
root2.left.right = new TreeNode(4);
root2.right.right = new TreeNode(7);
console.log("Input:", JSON.stringify(root2));
const key2 = 0;
const updatedRoot2 = deleteNode(root2, key2);
console.log("Output:", JSON.stringify(updatedRoot2));
const root3 = null;
console.log("Input:", root3);
const key3 = 0;
const updatedRoot3 = deleteNode(root3, key3);
console.log("Output:", updatedRoot3);