Complete the code to insert a value into a Binary Search Tree (BST).
function insertBST(root: TreeNode | null, val: number): TreeNode {
if (root === null) {
return new TreeNode(val);
}
if (val [1] root.val) {
root.left = insertBST(root.left, val);
} else {
root.right = insertBST(root.right, val);
}
return root;
}In a BST, values less than the current node go to the left subtree.
Complete the code to check if a key exists in a Hash Map.
function hasKey(map: Map<string, number>, key: string): boolean {
return map.[1](key);
}The has method checks if a key exists in a Map.
Fix the error in the code to find the minimum value in a BST.
function findMin(root: TreeNode | null): number | null {
if (root === null) return null;
let current = root;
while (current.[1] !== null) {
current = current.left;
}
return current.val;
}The minimum value in a BST is found by going left until no more left child exists.
Fill both blanks to create a function that returns keys in sorted order from a BST.
function inorderTraversal(root: TreeNode | null, result: number[] = []): number[] {
if (root !== null) {
inorderTraversal(root.[1], result);
result.push(root.val);
inorderTraversal(root.[2], result);
}
return result;
}Inorder traversal visits left subtree, then node, then right subtree, producing sorted keys.
Fill all three blanks to create a function that counts keys in a Hash Map greater than a threshold.
function countGreaterThan(map: Map<string, number>, threshold: number): number {
let count = 0;
for (const [1] of map.entries()) {
const [key, value] = [2];
if (value [3] threshold) {
count++;
}
}
return count;
}We iterate over entries, unpack each entry, and count values greater than threshold.
