Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to insert a value into the BST.
DSA C++
struct TreeNode {
int val;
TreeNode* left;
TreeNode* right;
TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
};
TreeNode* insertNode(TreeNode* root, int val) {
if (root == nullptr) {
return new TreeNode(val);
}
if (val < root->val) {
root->left = insertNode(root->left, [1]);
} else {
root->right = insertNode(root->right, val);
}
return root;
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using root->val instead of val causes wrong recursion.
Passing nullptr instead of val breaks insertion.
✗ Incorrect
When inserting, we recursively call insertNode on the left subtree with the value to insert.
2fill in blank
mediumComplete the code to perform inorder traversal and store values in a vector.
DSA C++
void inorderTraversal(TreeNode* root, std::vector<int>& vals) {
if (root == nullptr) return;
inorderTraversal(root->left, vals);
vals.push_back([1]);
inorderTraversal(root->right, vals);
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Adding root instead of root->val causes type errors.
Adding root->left instead of root->val adds wrong data.
✗ Incorrect
In inorder traversal, we add the current node's value to the vector.
3fill in blank
hardFix the error in the two-pointer search for the target sum in the sorted vector.
DSA C++
bool findTargetSum(const std::vector<int>& vals, int target) {
int left = 0, right = vals.size() - 1;
while (left < right) {
int sum = vals[left] + vals[[1]];
if (sum == target) return true;
else if (sum < target) left++;
else right--;
}
return false;
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using vals[left] twice causes incorrect sums.
Using vals[0] or vals[target] causes out-of-bounds errors.
✗ Incorrect
We add vals[left] and vals[right] to check the sum.
4fill in blank
hardFill both blanks to complete the function that checks if the BST has two elements summing to target.
DSA C++
bool findTarget(TreeNode* root, int k) {
std::vector<int> vals;
inorderTraversal(root, vals);
int left = 0, right = vals.size() - 1;
while (left < right) {
int sum = vals[left] + vals[right];
if (sum == k) return true;
else if (sum [1] k) left++;
else right[2];
}
return false;
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using > instead of < in the first blank reverses logic.
Using ++ on right pointer causes infinite loop.
✗ Incorrect
If sum is less than k, move left pointer up; else move right pointer down.
5fill in blank
hardFill all three blanks to build a complete two-sum in BST function using inorder traversal and two-pointer technique.
DSA C++
bool findTarget(TreeNode* root, int k) {
std::vector<int> vals;
[1](root, vals);
int left = 0, right = vals.size() - 1;
while (left < right) {
int sum = vals[left] + vals[right];
if (sum == k) return true;
else if (sum < k) left[2];
else right[3];
}
return false;
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Calling insertNode instead of inorderTraversal.
Using wrong pointer increments causing infinite loops.
✗ Incorrect
We call inorderTraversal to fill vals, then move pointers correctly to find the sum.