Complete the code to insert a key into a Binary Search Tree (BST).
if (root == nullptr) { root = new Node([1]); }
The BST node is created with the key value when the root is empty.
Complete the code to check if a key exists in a hash map.
if (hashMap.find([1]) != hashMap.end()) { // key exists }
We check if the key is present in the hash map by searching for it.
Fix the error in the BST in-order traversal code to print keys in ascending order.
void inorder(Node* root) {
if (root == nullptr) return;
inorder(root->[1]);
std::cout << root->key << " ";
inorder(root->right);
}In-order traversal visits the left child first to print keys in ascending order.
Fill both blanks to create a hash map that stores keys and their counts only if count is greater than 1.
std::unordered_map<int, int> counts; for (int num : nums) { counts[num]++; } auto filtered = [1]; for (auto& pair : counts) { if (pair.second [2] 1) { filtered[pair.first] = pair.second; } }
We initialize an empty unordered_map for filtered results and keep counts greater than 1.
Fill all three blanks to create a BST node insertion function that inserts keys smaller to the left and larger to the right.
Node* insert(Node* root, int key) {
if (root == nullptr) {
return new Node([1]);
}
if (key [2] root->key) {
root->left = insert(root->left, key);
} else if (key [3] root->key) {
root->right = insert(root->right, key);
}
return root;
}The function inserts a new node with the key, placing smaller keys to the left and larger keys to the right.