Depth-First Search
Path Sum II
DESCRIPTION (inspired by Leetcode.com)
Given the root of a binary tree and an integer target, write a recursive function to find all root-to-leaf paths where the sum of all the values along the path sum to target.
Example 1:
Input:
[1,2,4,4,7,5,1] target = 10
Output:
[[1,2,7],[1,4,5]] # [[1,4,5],[1,2,7]] is also accepted.
The paths are 1 -> 2 -> 7 and 1 -> 4 -> 5
Explanation
- The remaining target sum
- The values along the current path (starting from the root).
def dfs(node, target, path):# base caseif not node:return# append current value to the pathpath.append(node.val)if not node.left and not node.right:if node.val == target:result.append(path[:])dfs(node.left, target - node.val, path)dfs(node.right, target - node.val, path)# when our code reaches here, are done exploring all# the root-to-leaf paths that go through the current node.# pop the current value from the path to prepare for the next pathpath.pop()
Solution
class Solution:def pathSum(self, root, target):def dfs(node, target, path):# base caseif not node:return# append current value to the pathpath.append(node.val)if not node.left and not node.right:if node.val == target:result.append(path[:])dfs(node.left, target - node.val, path)dfs(node.right, target - node.val, path)# when our code reaches here, are done exploring all# the root-to-leaf paths that go through the current node.# pop the current value from the path to prepare for the next pathpath.pop()result = []dfs(root, target, [])return result
Animated Solution
def pathSum(root, target):def dfs(node, target, path):if not node:returnpath.append(node.val)if not node.left and not node.right:if node.val == target:result.append(path[:])dfs(node.left, target - node.val, path)dfs(node.right, target - node.val, path)path.pop()result = []dfs(root, target, [])return result
path sum II
0 / 41
Test Your Knowledge
Login to take the complexity quiz and track your progress
Complexity Analysis
Time Complexity: O(N²) where N is the number of nodes in the binary tree. We visit each node exactly once, which is O(N). However, when we find a valid root-to-leaf path, we copy it into the result list, which costs up to O(H) where H is the tree height. With K valid paths, the total copy cost is O(K·H), giving us O(N + K·H) overall. In the worst case: a 'caterpillar' tree where a long spine of N/2 nodes each has a leaf child — both K and H are O(N), so this becomes O(N²). Note that a fully balanced tree gives O(N·log N) since H = log N, and a fully skewed tree (a straight chain) gives O(N) since K = 1. The O(N²) worst case comes from tree shapes in between these extremes.
Space Complexity: O(N²) where N is the number of nodes in the binary tree in the worst case. The recursion stack uses O(H) space and the current path list uses O(H) space where H is the tree height. The result stores K valid paths of up to length H, which is O(K·H). In the worst case: the same 'caterpillar' tree shape would total to O(N²).
Mark as read
Unlock Premium Coding Content
On This Page
Your account is free and you can post anonymously if you choose.