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 Java code that checks if there is a root-to-leaf path with sum 22?
DSA Java
class TreeNode { int val; TreeNode left, right; TreeNode(int val) { this.val = val; } } public class Solution { public boolean hasPathSum(TreeNode root, int sum) { if (root == null) return false; if (root.left == null && root.right == null) return sum == root.val; return hasPathSum(root.left, sum - root.val) || hasPathSum(root.right, sum - root.val); } public static void main(String[] args) { TreeNode root = new TreeNode(5); root.left = new TreeNode(4); root.right = new TreeNode(8); root.left.left = new TreeNode(11); root.left.left.left = new TreeNode(7); root.left.left.right = new TreeNode(2); root.right.left = new TreeNode(13); root.right.right = new TreeNode(4); root.right.right.right = new TreeNode(1); Solution sol = new Solution(); System.out.println(sol.hasPathSum(root, 22)); } }
Attempts:
2 left
💡 Hint
Trace the path sums from root to leaves and check if any equals 22.
✗ Incorrect
The path 5 -> 4 -> 11 -> 2 sums to 22, so the output is true.
❓ Predict Output
intermediateOutput when no path matches the sum
What does the following code print when checking for a path sum of 26?
DSA Java
class TreeNode { int val; TreeNode left, right; TreeNode(int val) { this.val = val; } } public class Solution { public boolean hasPathSum(TreeNode root, int sum) { if (root == null) return false; if (root.left == null && root.right == null) return sum == root.val; return hasPathSum(root.left, sum - root.val) || hasPathSum(root.right, sum - root.val); } public static void main(String[] args) { TreeNode root = new TreeNode(1); root.left = new TreeNode(2); root.right = new TreeNode(3); Solution sol = new Solution(); System.out.println(sol.hasPathSum(root, 26)); } }
Attempts:
2 left
💡 Hint
Check all root-to-leaf paths and their sums.
✗ Incorrect
The only paths are 1->2=3 and 1->3=4, neither equals 26, so output is false.
🔧 Debug
advancedIdentify the error causing incorrect path sum result
The following code is intended to check if a root-to-leaf path sums to a target. It always returns false. What is the bug?
DSA Java
class TreeNode { int val; TreeNode left, right; TreeNode(int val) { this.val = val; } } public class Solution { public boolean hasPathSum(TreeNode root, int sum) { if (root == null) return false; if (root.left == null && root.right == null) return sum != root.val; return hasPathSum(root.left, sum - root.val) || hasPathSum(root.right, sum - root.val); } }
Attempts:
2 left
💡 Hint
Check the condition that returns true at leaf nodes.
✗ Incorrect
The leaf node check wrongly returns false when sum equals root.val, causing always false.
🧠 Conceptual
advancedUnderstanding path sum with negative values
Consider a binary tree where nodes can have negative values. Which statement about the path sum algorithm is true?
Attempts:
2 left
💡 Hint
Think about how the algorithm subtracts node values from sum.
✗ Incorrect
The algorithm subtracts node values from sum and checks leaf equality; negative values do not break this logic.
🚀 Application
expertNumber of root-to-leaf paths with given sum
Given the following code that counts the number of root-to-leaf paths with sum equal to target, what is the output for the tree and target 22?
DSA Java
class TreeNode { int val; TreeNode left, right; TreeNode(int val) { this.val = val; } } public class Solution { public int countPathSum(TreeNode root, int sum) { if (root == null) return 0; if (root.left == null && root.right == null) return sum == root.val ? 1 : 0; return countPathSum(root.left, sum - root.val) + countPathSum(root.right, sum - root.val); } public static void main(String[] args) { TreeNode root = new TreeNode(5); root.left = new TreeNode(4); root.right = new TreeNode(8); root.left.left = new TreeNode(11); root.left.left.left = new TreeNode(7); root.left.left.right = new TreeNode(2); root.right.left = new TreeNode(13); root.right.right = new TreeNode(4); root.right.right.left = new TreeNode(5); root.right.right.right = new TreeNode(1); Solution sol = new Solution(); System.out.println(sol.countPathSum(root, 22)); } }
Attempts:
2 left
💡 Hint
Count all root-to-leaf paths that sum to 22.
✗ Incorrect
There are two paths: 5->4->11->2 and 5->8->4->5, so output is 2.
