-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path450-delete-node-in-a-bst.js
More file actions
77 lines (68 loc) · 1.7 KB
/
Copy path450-delete-node-in-a-bst.js
File metadata and controls
77 lines (68 loc) · 1.7 KB
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
function TreeNode(val, left, right) {
this.val = (val === undefined ? 0 : val)
this.left = (left === undefined ? null : left)
this.right = (right === undefined ? null : right)
}
function arrayToTree(nums) {
if (nums.length === 0) {
return null;
}
let root = new TreeNode(nums[0]);
let q = [root];
let i = 1;
while (i < nums.length) {
let curr = q.shift();
if (i < nums.length) {
const val = nums[i++];
if (val) {
curr.left = new TreeNode(val);
}
q.push(curr.left);
}
if (i < nums.length) {
const val = nums[i++];
if (val) {
curr.right = new TreeNode(val);
}
q.push(curr.right);
}
}
return root;
}
function findMin(root) {
let min = root;
while (min.left) min = min.left;
return min.val;
}
function findMax(root) {
let max = root;
while (max.right) max = max.right;
return max.val;
}
function deleteNode(root, key) {
if (!root) {
return null;
}
if (key < root.val) {
root.left = deleteNode(root.left, key);
} else if (key > root.val) {
root.right = deleteNode(root.right, key);
} else {
if (!root.right) {
return root.left;
} else if (!root.left) {
return root.right;
}
const min = findMin(root.right);
root.val = min;
root.right = deleteNode(root.right, min);
}
return root;
}
// const root = arrayToTree([5, 3, 6, 2, 4, null, 7]);
const root = new TreeNode(4);
root.left = new TreeNode(3, new TreeNode(2));
root.right = new TreeNode(6, new TreeNode(5), new TreeNode(7));
console.log(JSON.stringify(root, (_, value) => value ?? undefined, 2));
console.log(JSON.stringify(deleteNode(root, 4), (_, value) => value ?? undefined, 2));
process.exit(0)