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 C# code that checks if a binary tree has a root-to-leaf path with sum 22?
DSA C#
public class TreeNode { public int val; public TreeNode left, right; public TreeNode(int val=0, TreeNode left=null, TreeNode right=null) { this.val = val; this.left = left; this.right = right; } } public class Solution { public bool HasPathSum(TreeNode root, int targetSum) { if (root == null) return false; if (root.left == null && root.right == null) return root.val == targetSum; return HasPathSum(root.left, targetSum - root.val) || HasPathSum(root.right, targetSum - root.val); } } // Tree construction TreeNode root = new TreeNode(5, new TreeNode(4, new TreeNode(11, new TreeNode(7), new TreeNode(2) ), null ), new TreeNode(8, new TreeNode(13), new TreeNode(4, null, new TreeNode(1) ) ) ); Solution sol = new Solution(); System.Console.WriteLine(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 in the given tree?
DSA C#
TreeNode root = new TreeNode(1, new TreeNode(2), new TreeNode(3) ); Solution sol = new Solution(); System.Console.WriteLine(sol.HasPathSum(root, 26));
Attempts:
2 left
💡 Hint
Check if any root-to-leaf path sums to 26.
✗ Incorrect
The only paths are 1->2=3 and 1->3=4, none equals 26, so output is False.
🔧 Debug
advancedIdentify the error in path sum code
What error does this code produce when run?
DSA C#
public bool HasPathSum(TreeNode root, int targetSum) { if (root == null) return false; if (root.left == null && root.right == null) return root.val == targetSum; return HasPathSum(root.left, targetSum - root.val) & HasPathSum(root.right, targetSum - root.val); }
Attempts:
2 left
💡 Hint
Check the operator used to combine recursive calls.
✗ Incorrect
The code uses & (bitwise AND) instead of || (logical OR), causing wrong results.
🧠 Conceptual
advancedNumber of root-to-leaf paths in a full binary tree of height 3
How many root-to-leaf paths are there in a full binary tree of height 3?
Attempts:
2 left
💡 Hint
A full binary tree has 2 children per node except leaves.
✗ Incorrect
Height 3 means 3 edges from root to leaf, so leaves count is 2^(height-1) = 4.
🚀 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 targetSum?
Attempts:
2 left
💡 Hint
Make sure to add a copy of the path, not the original list, to the result.
✗ Incorrect
Option A correctly adds a copy of the current path using ToList(), preserving the path state.
