Path Sum
Path Sum is LeetCode 112 (Easy). You get the root of a binary tree and an integer targetSum. Return true if some root-to-leaf path has values that add up to targetSum, and false otherwise.
- A leaf is a node with no children. A path starts at the root and ends at a leaf; stopping halfway does not count.
- Values and the target can be negative (−1000 to 1000), and the tree has up to 5,000 nodes.
- The number of nodes in the tree is in the range [0, 5000].
- -1000 <= Node.val <= 1000
- -1000 <= targetSum <= 1000
Intuition
Carry the amount still needed down the tree instead of a running total. At the root you need targetSum; once a path has passed a node with value v, the rest of it must supply targetSum - v. Each child gets the same, smaller question about its own subtree.
The question can only be answered at a leaf, where the path has to end: it succeeds exactly when the leaf's value equals what is still needed. The tree has a path if either subtree has one, so the first success can stop the whole search.
Any "from the root to a leaf" question is top-down recursion: pass a value into each call and decide at the leaves. This problem is the simplest of the family. Path Sum II (113) collects the matching paths, and Path Sum III (437) drops the root and leaf rule, which needs prefix sums instead.
Approach
Before reading on: for the tree [1, 2] with target 1, the root alone adds up to 1. Should the answer be true? Decide what counts as the end of a path before writing the base cases.
Stop at an empty spot
If root is None, return false. An empty spot has no path through it, so it can never complete the sum.
Answer at a leaf
If the node has no left and no right child, it is a leaf and the path must end here: return targetSum == root.val. Check this before recursing, so a node with one child is never treated as the end of a path.
Pass the remainder to both children
Compute rest = targetSum - root.val and return hasPathSum(root.left, rest) or hasPathSum(root.right, rest). The or stops early: once the left side finds a path, the right side is never searched.
Code in Python, C++ and Java
or stops here and the right subtree is never searched. Return it to the parent.or stops here and the right subtree (8) is never searched. The root returns true: a root-to-leaf path adds up to 22.Common pitfalls
Treating a node with one child as a leaf
if not root.left or not root.right:
return targetSum == root.valif not root.left and not root.right:
return targetSum == root.valWith or, a node that still has a child ends the path early, so a path that never reached a leaf can be reported. A leaf needs both children missing.
Checking the sum at an empty child
if not root:
return targetSum == 0if not root:
return FalseTake [1, 2] with target 1. After the root the remainder is 0, so the empty right child of 1 would report success, although 1 is not a leaf and the only real path, 1 → 2, sums to 3.
Giving up when the remainder goes negative
rest = targetSum - root.val
if rest < 0:
return Falserest = targetSum - root.val return self.hasPathSum(root.left, rest) or ...
Values can be negative, so a later node can bring the sum back. In [1, -2] with target -1 the remainder after the root is -2, and the leaf -2 matches it exactly.
Edge cases
root is None, so the first check returns false, even when targetSum is 0: there is no path at all. This is LeetCode's third example.
Complexity
or can stop it early. The call stack holds one call per level of the tree: O(log n) when the tree is balanced, up to O(n) on a single chain.Path Sum FAQ
Can the Python solution avoid recursion?
Yes. Keep a stack of (node, sum so far) pairs, starting with (root, root.val). Pop a pair; if the node is a leaf and the sum equals targetSum, return True; otherwise push each child with the sum plus the child's value. Return False when the stack is empty. The time is still O(n); the list replaces the call stack.