Bird
Raised Fist0
DSA Javaprogramming~10 mins

Two Sum in BST in DSA Java - Interactive Practice

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

Complete the code to check if the BST node is null.

DSA Java
if (root [1] null) {
    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 '==' causes assignment, not comparison.
Using '!=' reverses the logic.
2fill in blank
medium

Complete the code to add a value to the HashSet.

DSA Java
set.[1](root.val);
Drag options to blanks, or click blank then click option'
Aremove
Badd
Ccontains
Dclear
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'contains' instead of 'add' does not insert the value.
Using 'remove' deletes elements, which is incorrect here.
3fill in blank
hard

Fix the error in the condition to check if the complement exists in the set.

DSA Java
if (set.[1](k - root.val)) {
    return true;
}
Drag options to blanks, or click blank then click option'
Acontains
Badd
Cremove
Dclear
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'add' instead of 'contains' changes the set and returns a boolean about insertion.
Using 'remove' deletes elements, which is not intended here.
4fill in blank
hard

Fill both blanks to complete the recursive calls for left and right subtrees.

DSA Java
return dfs(root.[1], set, k) || dfs(root.[2], set, k);
Drag options to blanks, or click blank then click option'
Aleft
Bright
Cval
Dparent
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'val' or 'parent' instead of 'left' or 'right' causes errors.
Swapping left and right does not cause errors but is less clear.
5fill in blank
hard

Fill all three blanks to complete the Two Sum in BST method using DFS and a HashSet.

DSA Java
public boolean findTarget(TreeNode root, int k) {
    Set<Integer> set = new HashSet<>();
    return dfs(root, [1], k);
}

private boolean dfs(TreeNode root, Set<Integer> [2], int k) {
    if (root == null) return false;
    if ([3].contains(k - root.val)) return true;
    [2].add(root.val);
    return dfs(root.left, [2], k) || dfs(root.right, [2], k);
}
Drag options to blanks, or click blank then click option'
Aset
Bvisited
Cnodes
Dvalues
Attempts:
3 left
💡 Hint
Common Mistakes
Using different variable names causes compilation errors.
Using undefined variables causes runtime errors.

Practice

(1/5)
1. What is the main goal of the Two Sum in BST problem?
easy
A. Count the total number of nodes in the BST.
B. Find the maximum value in the BST.
C. Find two nodes in the BST whose values add up to a target sum.
D. Check if the BST is balanced.

Solution

  1. Step 1: Understand the problem statement

    The problem asks to find if there exist two nodes in the BST such that their values add up to a given target.
  2. Step 2: Identify the goal

    The goal is to check for the existence of such a pair, not to find max, balance, or count nodes.
  3. Final Answer:

    Find two nodes in the BST whose values add up to a target sum. -> Option C
  4. Quick Check:

    Two Sum in BST means finding two nodes summing to target [OK]
Hint: Remember Two Sum means pair sums to target [OK]
Common Mistakes:
  • Confusing Two Sum with finding max value
  • Thinking problem is about BST balance
  • Counting nodes instead of finding pairs
2. Which of the following Java method signatures correctly represents a function to solve Two Sum in BST?
easy
A. public int findSum(TreeNode root, int k)
B. public boolean findTarget(TreeNode root, int k)
C. public void findTarget(TreeNode root, int k)
D. public TreeNode findPair(TreeNode root, int k)

Solution

  1. Step 1: Understand the return type needed

    The function should return true or false if a pair exists, so return type is boolean.
  2. Step 2: Check method signatures

    public boolean findTarget(TreeNode root, int k) returns boolean and accepts root and target, matching the problem requirements.
  3. Final Answer:

    public boolean findTarget(TreeNode root, int k) -> Option B
  4. Quick Check:

    Boolean return for existence check [OK]
Hint: Two Sum returns true/false, so use boolean return [OK]
Common Mistakes:
  • Using void return type instead of boolean
  • Returning int instead of boolean
  • Returning TreeNode instead of boolean
3. Given the BST below and target = 9, what will the output of findTarget(root, 9) be?

      5
     / \
    3   6
   / \   \
  2   4   7
medium
A. true
B. false
C. null
D. Runtime Error

Solution

  1. Step 1: Identify pairs that sum to 9

    Check pairs: 2+7=9, 3+6=9, 4+5=9. All valid pairs exist.
  2. Step 2: Confirm function returns true if any pair found

    Since pairs exist, function returns true.
  3. Final Answer:

    true -> Option A
  4. Quick Check:

    Pairs 2+7 or 3+6 sum to 9 [OK]
Hint: Check pairs adding to target in BST nodes [OK]
Common Mistakes:
  • Ignoring valid pairs and returning false
  • Confusing output with node values
  • Assuming null or error without reason
4. Consider this buggy code snippet for Two Sum in BST:

public boolean findTarget(TreeNode root, int k) {
    Set<Integer> set = new HashSet<>();
    return dfs(root, k, set);
}

private boolean dfs(TreeNode node, int k, Set<Integer> set) {
    if (node == null) return false;
    if (set.contains(k - node.val)) return true;
    set.add(node.val);
    dfs(node.left, k, set);
    dfs(node.right, k, set);
    return false;
}
What is the main bug causing incorrect results?
medium
A. Using HashSet instead of ArrayList.
B. Adding node values after checking set.
C. Not checking if root is null in findTarget.
D. Missing return statements from recursive calls in dfs.

Solution

  1. Step 1: Analyze dfs recursive calls

    dfs calls itself on left and right but ignores their return values.
  2. Step 2: Understand impact of missing returns

    Without returning results of recursive calls, function always returns false unless first node matches.
  3. Step 3: Correct fix

    Return true if either left or right dfs call returns true.
  4. Final Answer:

    Missing return statements from recursive calls in dfs. -> Option D
  5. Quick Check:

    Return recursive calls to propagate true [OK]
Hint: Always return recursive call results in DFS [OK]
Common Mistakes:
  • Ignoring return values of recursive calls
  • Using wrong data structure for lookup
  • Not handling null root properly
5. You want to optimize Two Sum in BST to use O(h) space, where h is tree height. Which approach is best?
hard
A. Use two iterators: one in-order and one reverse in-order traversal.
B. Store all node values in a list and use two pointers.
C. Use a HashSet to store visited nodes during DFS.
D. Convert BST to sorted array and binary search for complements.

Solution

  1. Step 1: Understand space complexity of approaches

    Storing all nodes uses O(n) space; HashSet also O(n); array conversion O(n).
  2. Step 2: Two iterator approach

    Using two iterators (in-order and reverse in-order) simulates two pointers with O(h) space.
  3. Step 3: Choose best approach

    Use two iterators: one in-order and one reverse in-order traversal. uses O(h) space and is optimal for BST Two Sum.
  4. Final Answer:

    Use two iterators: one in-order and one reverse in-order traversal. -> Option A
  5. Quick Check:

    Two iterators give O(h) space [OK]
Hint: Two pointers in BST use two iterators for O(h) space [OK]
Common Mistakes:
  • Using full list storage causing O(n) space
  • Relying on HashSet which uses O(n) space
  • Ignoring BST properties for optimization
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard