Complete the code to check if the current node is None in the delete function.
if root [1] { return root; }
We check if the root is None to stop recursion when the node is not found.
Complete the code to move left or right in the BST based on the key comparison.
if key < root.as_ref().unwrap().val { root.as_mut().unwrap().left = Self::delete_node(root.as_mut().unwrap().left.take(), key); } else if key > root.as_ref().unwrap().val { root.as_mut().unwrap().right = Self::delete_node(root.as_mut().unwrap().right.take(), [1]); }
We continue searching for the key to delete by comparing with the current node's value.
Fix the error in the code to find the minimum node in the right subtree.
fn min_value_node(mut node: Option<Box<TreeNode>>) -> Option<Box<TreeNode>> {
while node.as_ref().unwrap().left.is_some() {
node = node.as_mut().unwrap().left.take();
}
[1]
}Return the node itself after reaching the leftmost node.
Fill both blanks to replace the current node with its right child when left child is None.
if root.as_ref().unwrap().left.is_none() { let temp = root.as_mut().unwrap().right.take(); [1] = None; [2] = temp; return [2]; }
We set root to None and then assign temp (right child) to root to delete the current node.
Fill all three blanks to replace the current node's value with the minimum node's value and delete that minimum node.
let min_node = Self::min_value_node(root.as_ref().unwrap().right.clone()).unwrap(); root.as_mut().unwrap().val = [1]; root.as_mut().unwrap().right = Self::delete_node(root.as_mut().unwrap().right.take(), [2]); return [3];
We copy the minimum node's value to current node, delete the minimum node from right subtree, and return root.
