Complete the code to start searching from the root node.
function searchBST(root, val) {
let current = [1];
while (current !== null) {
if (val === current.val) return current;
else if (val < current.val) current = current.left;
else current = current.right;
}
return null;
}We start searching from the root node, so current should be initialized to root.
Complete the code to move left when the search value is smaller than current node's value.
if (val < current.val) { current = [1]; }
If the value to find is less than the current node's value, we go to the left child.
Fix the error in the condition to check if the current node is null.
while ([1] !== null) { if (val === current.val) return current; else if (val < current.val) current = current.left; else current = current.right; }
The loop should continue while current is not null, meaning there are still nodes to check.
Fill both blanks to complete the recursive search function for BST.
function searchBST(node, val) {
if (node === null || node.val === [1]) return node;
if (val < node.val) return searchBST(node.[2], val);
else return searchBST(node.right, val);
}The base case checks if the current node's value equals val. If not, and val is smaller, we search the left subtree.
Fill all three blanks to create a function that returns true if val exists in BST, false otherwise.
function existsInBST(root, val) {
if (root === null) return false;
if (root.val === [1]) return true;
if (val < root.val) return existsInBST(root.[2], [3]);
else return existsInBST(root.right, val);
}Check if the current node's value equals val. If not, and val is smaller, search the left subtree recursively.