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_in_bst function?
DSA Python
class TreeNode: def __init__(self, val=0, left=None, right=None): self.val = val self.left = left self.right = right def two_sum_in_bst(root, target): seen = set() def dfs(node): if not node: return False if target - node.val in seen: return True seen.add(node.val) return dfs(node.left) or dfs(node.right) return dfs(root) # BST structure: # 5 # / \ # 3 6 # / \ \ # 2 4 7 root = TreeNode(5, TreeNode(3, TreeNode(2), TreeNode(4)), TreeNode(6, None, TreeNode(7))) print(two_sum_in_bst(root, 9))
Attempts:
2 left
š” Hint
Check if any two nodes sum up to 9 by traversing the tree and storing visited values.
ā Incorrect
The function finds two nodes with values 2 and 7 that sum to 9, so it returns True.
ā Predict Output
intermediateOutput of Two Sum in BST with target 28
What is the output of the two_sum_in_bst function for the BST below and target = 28?
DSA Python
class TreeNode: def __init__(self, val=0, left=None, right=None): self.val = val self.left = left self.right = right def two_sum_in_bst(root, target): seen = set() def dfs(node): if not node: return False if target - node.val in seen: return True seen.add(node.val) return dfs(node.left) or dfs(node.right) return dfs(root) # BST structure: # 10 # / \ # 5 15 # / \ \ # 3 7 18 root = TreeNode(10, TreeNode(5, TreeNode(3), TreeNode(7)), TreeNode(15, None, TreeNode(18))) print(two_sum_in_bst(root, 28))
Attempts:
2 left
š” Hint
Check if any two nodes sum up to 28 by traversing the tree and storing visited values.
ā Incorrect
The function finds two nodes with values 10 and 18 that sum to 28, so it returns True.
š§ Debug
advancedIdentify the error in Two Sum in BST implementation
What error will this code raise when run?
DSA Python
class TreeNode: def __init__(self, val=0, left=None, right=None): self.val = val self.left = left self.right = right def two_sum_in_bst(root, target): seen = set() def dfs(node): if not node: return False if target - node.val in seen: return True seen.add(node.val) return dfs(node.left) or dfs(node.right) return dfs(root) root = TreeNode(1) print(two_sum_in_bst(root, '3'))
Attempts:
2 left
š” Hint
Check the types of target and node values during subtraction.
ā Incorrect
Subtracting string '3' and int node.val causes a TypeError.
š§ Conceptual
advancedWhy use a set in Two Sum in BST?
Why does the two_sum_in_bst function use a set to store seen values?
Attempts:
2 left
š” Hint
Think about how to check if a number exists efficiently.
ā Incorrect
A set allows fast lookup to check if the complement of the current node's value has been seen before.
š Application
expertNumber of pairs summing to target in BST
Given the BST below and target = 10, how many unique pairs of nodes sum to the target?
DSA Python
class TreeNode: def __init__(self, val=0, left=None, right=None): self.val = val self.left = left self.right = right def count_pairs_in_bst(root, target): seen = set() pairs = set() def dfs(node): if not node: return complement = target - node.val if complement in seen and (min(node.val, complement), max(node.val, complement)) not in pairs: pairs.add((min(node.val, complement), max(node.val, complement))) seen.add(node.val) dfs(node.left) dfs(node.right) dfs(root) return len(pairs) # BST structure: # 5 # / \ # 3 7 # / \ \ # 2 4 8 root = TreeNode(5, TreeNode(3, TreeNode(2), TreeNode(4)), TreeNode(7, None, TreeNode(8))) print(count_pairs_in_bst(root, 10))
Attempts:
2 left
š” Hint
Find pairs like (2,8) and (3,7) that sum to 10.
ā Incorrect
Pairs (2,8) and (3,7) sum to 10, so the count is 2.
