Path Sum II
LeetCode 113 | Difficulty: Mediumβ
MediumProblem Descriptionβ
Given the root of a binary tree and an integer targetSum, return all root-to-leaf paths where the sum of the node values in the path equals targetSum. Each path should be returned as a list of the node values, not node references.
A root-to-leaf path is a path starting from the root and ending at any leaf node. A leaf is a node with no children.
Example 1:

Input: root = [5,4,8,11,null,13,4,7,2,null,null,5,1], targetSum = 22
Output: [[5,4,11,2],[5,8,4,5]]
Explanation: There are two paths whose sum equals targetSum:
5 + 4 + 11 + 2 = 22
5 + 8 + 4 + 5 = 22
Example 2:

Input: root = [1,2,3], targetSum = 5
Output: []
Example 3:
Input: root = [1,2], targetSum = 0
Output: []
Constraints:
- The number of nodes in the tree is in the range `[0, 5000]`.
- `-1000 <= Node.val <= 1000`
- `-1000 <= targetSum <= 1000`
Topics: Backtracking, Tree, Depth-First Search, Binary Tree
Approachβ
Backtrackingβ
Explore all candidates by building solutions incrementally. At each step, choose an option, explore further, then unchoose (backtrack) to try the next option. Prune branches that can't lead to valid solutions.
Generate all combinations/permutations, or find solutions that satisfy constraints.
Tree DFSβ
Traverse the tree recursively (or with a stack). At each node, decide: what information do I need from the left/right subtrees? Process: go left β go right β combine results. Consider preorder, inorder, or postorder traversal based on when you need to process the node.
Path problems, subtree properties, tree structure manipulation.
Solutionsβ
Solution 1: C# (Best: 296 ms)β
| Metric | Value |
|---|---|
| Runtime | 296 ms |
| Memory | N/A |
| Date | 2018-04-10 |
/**
* Definition for a binary tree node.
* public class TreeNode {
* public int val;
* public TreeNode left;
* public TreeNode right;
* public TreeNode(int x) { val = x; }
* }
*/
public class Solution {
public IList<IList<int>> PathSum(TreeNode root, int sum) {
IList<IList<int>> dp = new List<IList<int>>();
List<int> temp = new List<int>();
PathSumUtil(root, dp, temp, sum);
return dp;
}
public void PathSumUtil(TreeNode root, IList<IList<int>> dp, List<int> temp, int sum)
{
if(root==null)
return;
temp.Add(root.val);
if(sum==root.val && root.left ==null && root.right==null)
{
dp.Add(temp.ToList());
}
else
{
if (root.left != null)
{
PathSumUtil(root.left, dp, temp, sum - root.val);
}
if (root.right != null)
{
PathSumUtil(root.right, dp, temp, sum - root.val);
}
}
temp.RemoveAt(temp.Count - 1);
}
}
π 1 more C# submission(s)
Submission (2018-04-10) β 300 ms, N/Aβ
/**
* Definition for a binary tree node.
* public class TreeNode {
* public int val;
* public TreeNode left;
* public TreeNode right;
* public TreeNode(int x) { val = x; }
* }
*/
public class Solution {
public IList<IList<int>> PathSum(TreeNode root, int sum) {
IList<IList<int>> dp = new List<IList<int>>();
List<int> temp = new List<int>();
PathSumUtil(root, dp, temp, sum);
return dp;
}
public void PathSumUtil(TreeNode root, IList<IList<int>> dp, List<int> temp, int sum)
{
if(root==null)
return;
temp.Add(root.val);
if(sum==root.val && root.left ==null && root.right==null)
{
dp.Add(temp.ToList());
}
else
{
PathSumUtil(root.left, dp, temp, sum - root.val);
PathSumUtil(root.right, dp, temp, sum - root.val);
}
temp.RemoveAt(temp.Count - 1);
}
}
Complexity Analysisβ
| Approach | Time | Space |
|---|---|---|
| Backtracking | $O(n! or 2^n)$ | $O(n)$ |
| Tree Traversal | $O(n)$ | $O(h)$ |
Interview Tipsβ
- Discuss the brute force approach first, then optimize. Explain your thought process.
- Consider: "What information do I need from each subtree?" β this defines your recursive return value.
- Identify pruning conditions early to avoid exploring invalid branches.