Complete the code to check if the current node is null before deletion.
if (root === [1]) { return root; }
We check if the root is null to know if the tree or subtree is empty, so we return it as is.
Complete the code to move left or right in the BST based on the key value.
if (key < root.val) { root.left = deleteNode(root.left, [1]); } else if (key > root.val) { root.right = deleteNode(root.right, key); }
We pass the key down the tree to find the node to delete.
Fix the error in finding the minimum value node in the right subtree.
let minNode = root.right; while (minNode.[1] !== null) { minNode = minNode.left; }
To find the minimum node in the right subtree, we keep going left until there is no left child. The loop condition checks if minNode.left is not null to continue.
Fill both blanks to replace the node's value with the minimum node's value and delete the minimum node.
root.val = minNode.[1]; root.right = deleteNode(root.right, [2]);
We copy the minimum node's value to the current node, then delete the minimum node by passing its value to the delete function.
Fill all three blanks to handle the case when the node to delete has only one child.
if (root.left === null) { return root.[1]; } else if (root.right === null) { return root.[2]; } // Node with two children handled later return [3];
If the node has no left child, we return its right child to replace it. If no right child, we return its left child. Otherwise, we return the current root after handling two children case.