Challenge - 5 Problems
Two Sum in BST Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of Two Sum in BST with target 9
Given the BST below and target = 9, what is the output of the Two Sum function that returns true if two nodes sum to the target?
DSA Java
class TreeNode { int val; TreeNode left, right; TreeNode(int x) { val = x; } } public class Solution { 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); return dfs(node.left, k, set) || dfs(node.right, k, set); } } // BST: // 5 // / \ // 3 6 // / \ \ // 2 4 7
Attempts:
2 left
💡 Hint
Check if any two nodes add up to 9.
✗ Incorrect
The nodes 2 and 7 sum to 9, so the function returns true.
❓ Predict Output
intermediateOutput of Two Sum in BST with target 28
Given the same BST as before and target = 28, what is the output of the Two Sum function?
DSA Java
class TreeNode { int val; TreeNode left, right; TreeNode(int x) { val = x; } } public class Solution { 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); return dfs(node.left, k, set) || dfs(node.right, k, set); } } // BST: // 5 // / \ // 3 6 // / \ \ // 2 4 7
Attempts:
2 left
💡 Hint
No two nodes sum to 28.
✗ Incorrect
No pair of nodes in the BST sums to 28, so the function returns false.
🔧 Debug
advancedIdentify the error in Two Sum BST code
What error does the following code produce when run on a BST with target 10?
DSA Java
class TreeNode { int val; TreeNode left, right; TreeNode(int x) { val = x; } } public class Solution { 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; } }
Attempts:
2 left
💡 Hint
Check the return statements in recursion.
✗ Incorrect
The recursive calls do not return their results, so the function always returns false.
🧠 Conceptual
advancedWhy use a HashSet in Two Sum BST?
Why is a HashSet used in the Two Sum in BST algorithm instead of just searching the BST for complements?
Attempts:
2 left
💡 Hint
Think about lookup times.
✗ Incorrect
Using a HashSet allows quick checks for complements in constant time, avoiding repeated BST searches which take O(log n) each.
🚀 Application
expertNumber of pairs summing to target in BST
Given the BST below and target = 7, how many unique pairs of nodes sum to 7?
DSA Java
// BST: // 4 // / \ // 2 6 // / \ / \ // 1 3 5 7
Attempts:
2 left
💡 Hint
Find all pairs (a,b) where a + b = 7 and a < b.
✗ Incorrect
Pairs are (1,6), (2,5), and (3,4), totaling 3 pairs.
