Challenge - 5 Problems
Path Sum Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of path sum check for a simple tree
What is the output of the following code that checks if there is a root-to-leaf path with the given sum?
DSA Python
class TreeNode: def __init__(self, val=0, left=None, right=None): self.val = val self.left = left self.right = right def has_path_sum(root, target_sum): if not root: return False if not root.left and not root.right: return root.val == target_sum return has_path_sum(root.left, target_sum - root.val) or has_path_sum(root.right, target_sum - root.val) # Tree: # 5 # / \ # 4 8 # / / \ # 11 13 4 # Target sum = 22 root = TreeNode(5) root.left = TreeNode(4) root.right = TreeNode(8) root.left.left = TreeNode(11) root.right.left = TreeNode(13) root.right.right = TreeNode(4) print(has_path_sum(root, 22))
Attempts:
2 left
💡 Hint
Check if any root-to-leaf path sums exactly to 22.
✗ Incorrect
The root-to-leaf paths sum to 20 (5-4-11), 26 (5-8-13), and 17 (5-8-4). None equal 22, so output is False (option D).
❓ Predict Output
intermediateOutput of path sum check with negative values
What is the output of this code that checks for a path sum including negative values?
DSA Python
class TreeNode: def __init__(self, val=0, left=None, right=None): self.val = val self.left = left self.right = right def has_path_sum(root, target_sum): if not root: return False if not root.left and not root.right: return root.val == target_sum return has_path_sum(root.left, target_sum - root.val) or has_path_sum(root.right, target_sum - root.val) # Tree: # 1 # / \ # -2 -3 # / \ \ # 1 3 -2 # Target sum = -1 root = TreeNode(1) root.left = TreeNode(-2) root.right = TreeNode(-3) root.left.left = TreeNode(1) root.left.right = TreeNode(3) root.right.right = TreeNode(-2) print(has_path_sum(root, -1))
Attempts:
2 left
💡 Hint
Consider all root-to-leaf paths and their sums.
✗ Incorrect
The root-to-leaf paths sum to 0 (1--2-1), 2 (1--2-3), and -4 (1--3--2). None equal -1, so output is False (option A).
🔧 Debug
advancedIdentify the error in path sum function
What error does this code raise when checking for a path sum?
DSA Python
class TreeNode: def __init__(self, val=0, left=None, right=None): self.val = val self.left = left self.right = right def has_path_sum(root, target_sum): if root is None: return False if root.left is None and root.right is None: return root.val == target_sum return has_path_sum(root.left, target_sum - root.val) and has_path_sum(root.right, target_sum - root.val) root = TreeNode(1) root.left = TreeNode(2) root.right = TreeNode(3) print(has_path_sum(root, 3))
Attempts:
2 left
💡 Hint
Check the logical operator used to combine recursive calls.
✗ Incorrect
The function uses 'and' to combine left and right subtree results, but it should use 'or' because a path sum can be found in either subtree. Using 'and' means both subtrees must have a path sum, which is logically incorrect.
🧠 Conceptual
advancedNumber of root-to-leaf paths with given sum
Given a binary tree, how would you find the number of root-to-leaf paths where the sum of node values equals a target sum?
Attempts:
2 left
💡 Hint
Think about exploring all paths and counting matches at leaves.
✗ Incorrect
To count all root-to-leaf paths with a given sum, you must explore every path recursively and increment a counter when a leaf node's path sum matches the target. BFS stopping at first match or ignoring leaf nodes won't count all paths.
🚀 Application
expertFind all root-to-leaf paths with given sum
Which code snippet correctly returns all root-to-leaf paths where the sum of node values equals the target sum?
DSA Python
class TreeNode: def __init__(self, val=0, left=None, right=None): self.val = val self.left = left self.right = right # Choose the correct function implementation:
Attempts:
2 left
💡 Hint
Look for a solution that avoids recomputing sums repeatedly and backtracks properly.
✗ Incorrect
Option C uses DFS with backtracking, accumulating sum as it goes, and appends a copy of the path when the sum matches at a leaf. Option C misses adding root.val to paths. Option C uses stack but misses initial root check if root is None. Option C recomputes sum(current_path) repeatedly, which is inefficient.
