binary tree
7
11
2
4
5
13
8
4
1
call stack ↓
(returned)
Recursive DFS (carry remaining down)
▸1hasPathSum(node, remaining):2 remaining -= node.val3 if leaf: return remaining == 0 // base case4 if hasPathSum(node.left, remaining): return true5 if hasPathSum(node.right, remaining): return true6 return false // combine7// answer = hasPathSum(root, target)
state
- target22
- start remaining22