Watch 10 video solutions for Path Sum, a easy level problem involving Tree, Depth-First Search, Breadth-First Search. This walkthrough by Greg Hogg has 420,074 views views. Want to try solving it yourself? Practice on FleetCode or read the detailed text solution.
Given the root of a binary tree and an integer targetSum, return true if the tree has a root-to-leaf path such that adding up all the values along the path equals targetSum.
A leaf is a node with no children.
Example 1:
Input: root = [5,4,8,11,null,13,4,7,2,null,null,null,1], targetSum = 22 Output: true Explanation: The root-to-leaf path with the target sum is shown.
Example 2:
Input: root = [1,2,3], targetSum = 5 Output: false Explanation: There are two root-to-leaf paths in the tree: (1 --> 2): The sum is 3. (1 --> 3): The sum is 4. There is no root-to-leaf path with sum = 5.
Example 3:
Input: root = [], targetSum = 0 Output: false Explanation: Since the tree is empty, there are no root-to-leaf paths.
Constraints:
[0, 5000].-1000 <= Node.val <= 1000-1000 <= targetSum <= 1000Problem Overview: You are given the root of a binary tree and a target sum. The task is to determine whether the tree has a root-to-leaf path such that adding all node values along that path equals the target. A leaf means a node with no children. The main challenge is traversing the tree while continuously tracking the remaining sum.
Approach 1: Recursive Depth-First Search (DFS) (Time: O(n), Space: O(h))
The most natural way to solve this problem is recursion with Depth-First Search. Start at the root and subtract the current node's value from the target sum. Then recursively explore the left and right children with the updated remaining sum. If you reach a leaf node and the remaining sum equals that node's value, a valid path exists.
This works because DFS follows one path from root to leaf at a time, which matches the structure of the requirement. Each node is visited once, giving O(n) time where n is the number of nodes. The recursion stack stores the current path, leading to O(h) space where h is the height of the binary tree. This solution is clean, short, and widely used in interviews.
Approach 2: Iterative Depth-First Search with Stack (Time: O(n), Space: O(h))
If you want to avoid recursion, simulate DFS using an explicit stack. Store pairs of (node, remainingSum). Push the root with the initial target sum. On each iteration, pop a node, subtract its value, and check if it is a leaf with the correct remaining sum. If not, push its children onto the stack with the updated value.
This approach performs the same logical steps as recursive DFS but manages traversal manually. The stack ensures nodes are explored depth-first, just like recursion. Time complexity remains O(n) since each node is processed once. Space complexity is also O(h) because the stack holds at most the nodes along the current path. This method is useful in environments where recursion depth could be limited.
Both approaches rely on the properties of tree traversal. The key insight is that the running sum can be updated as you move down the tree instead of storing full paths.
Recommended for interviews: Recursive DFS is typically expected because it maps directly to the structure of the problem and produces concise code. Interviewers often want to see that you recognize root-to-leaf traversal as a DFS pattern. The iterative stack approach demonstrates deeper understanding of DFS mechanics and recursion elimination, which can be useful if the interviewer asks for an alternative implementation.
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Recursive Depth-First Search (DFS) | O(n) | O(h) | Best general solution for binary tree path problems. Clean and commonly expected in interviews. |
| Iterative DFS with Stack | O(n) | O(h) | When avoiding recursion or demonstrating explicit DFS traversal using a stack. |