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 check
What is the output of the following C# code that checks if there exist two nodes in the BST whose values sum to the target?
DSA C#
using System; using System.Collections.Generic; public class TreeNode { public int val; public TreeNode left, right; public TreeNode(int x) { val = x; } } public class Solution { public bool FindTarget(TreeNode root, int k) { var set = new HashSet<int>(); return DFS(root, k, set); } private bool DFS(TreeNode node, int k, HashSet<int> 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); } } public class Program { public static void Main() { var root = new TreeNode(5); root.left = new TreeNode(3); root.right = new TreeNode(6); root.left.left = new TreeNode(2); root.left.right = new TreeNode(4); root.right.right = new TreeNode(7); var sol = new Solution(); Console.WriteLine(sol.FindTarget(root, 9)); } }
Attempts:
2 left
💡 Hint
Think about whether any two nodes in the BST add up to 9.
✗ Incorrect
The nodes with values 2 and 7 sum to 9, so the output is True.
❓ Predict Output
intermediateOutput when no two nodes sum to target
What is the output of this C# code checking for two nodes summing to 28 in the BST?
DSA C#
using System; using System.Collections.Generic; public class TreeNode { public int val; public TreeNode left, right; public TreeNode(int x) { val = x; } } public class Solution { public bool FindTarget(TreeNode root, int k) { var set = new HashSet<int>(); return DFS(root, k, set); } private bool DFS(TreeNode node, int k, HashSet<int> 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); } } public class Program { public static void Main() { var root = new TreeNode(5); root.left = new TreeNode(3); root.right = new TreeNode(6); root.left.left = new TreeNode(2); root.left.right = new TreeNode(4); root.right.right = new TreeNode(7); var sol = new Solution(); Console.WriteLine(sol.FindTarget(root, 28)); } }
Attempts:
2 left
💡 Hint
Check if any two nodes sum to 28.
✗ Incorrect
No two nodes in the BST sum to 28, so the output is False.
🧠 Conceptual
advancedUnderstanding Two Sum in BST approach
Which of the following best describes the approach used to solve the Two Sum in BST problem in the given code?
Attempts:
2 left
💡 Hint
Think about how the code uses a HashSet during traversal.
✗ Incorrect
The code uses DFS traversal and a HashSet to check if the complement exists, achieving O(n) time complexity.
🔧 Debug
advancedIdentify the error in Two Sum in BST code
What error will this code produce when run?
DSA C#
using System; using System.Collections.Generic; public class TreeNode { public int val; public TreeNode left, right; public TreeNode(int x) { val = x; } } public class Solution { public bool FindTarget(TreeNode root, int k) { var set = new HashSet<int>(); return DFS(root, k, set); } private bool DFS(TreeNode node, int k, HashSet<int> 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); } } public class Program { public static void Main() { var root = new TreeNode(5); root.left = new TreeNode(3); root.right = new TreeNode(6); root.left.left = new TreeNode(2); root.left.right = new TreeNode(4); root.right.right = new TreeNode(7); var sol = new Solution(); Console.WriteLine(sol.FindTarget(root, 9)); } }
Attempts:
2 left
💡 Hint
Check the use of logical operators in recursion.
✗ Incorrect
Using && means both left and right must be true, but the search should return true if either side finds a pair. So it returns false if one side is false.
🚀 Application
expertCount pairs with sum in BST
Given a BST, how many unique pairs of nodes have values that sum to 10? Consider the BST below:
8
/ \
3 10
/ \ \
1 6 14
/ \ /
4 7 13
Choose the correct count of unique pairs.
Attempts:
2 left
💡 Hint
Look for pairs of nodes whose values add up to 10.
✗ Incorrect
The pairs are (3,7) and (4,6). No other pairs sum to 10 in the BST.
