0
0
DSA C++programming~10 mins

Two Sum in BST in DSA C++ - Interactive Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete 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'
Aval
Broot->val
Cnullptr
Droot
Attempts:
3 left
💡 Hint
Common Mistakes
Using root->val instead of val causes wrong recursion.
Passing nullptr instead of val breaks insertion.
2fill in blank
medium

Complete 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'
Aroot->val
Bvals
Croot->left
Droot
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.
3fill in blank
hard

Fix 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'
Aleft
Btarget
C0
Dright
Attempts:
3 left
💡 Hint
Common Mistakes
Using vals[left] twice causes incorrect sums.
Using vals[0] or vals[target] causes out-of-bounds errors.
4fill in blank
hard

Fill 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'
A<
B>
C--
D++
Attempts:
3 left
💡 Hint
Common Mistakes
Using > instead of < in the first blank reverses logic.
Using ++ on right pointer causes infinite loop.
5fill in blank
hard

Fill 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'
AinorderTraversal
B++
C--
DinsertNode
Attempts:
3 left
💡 Hint
Common Mistakes
Calling insertNode instead of inorderTraversal.
Using wrong pointer increments causing infinite loops.