Link: https://leetcode.com/problems/path-sum-ii/
Solution:
Topics: DFS, back tracking
Intuition
This is a simple DFS problem but with a notable edge case. The base-case must be triggered at the leaf node, not on a null pointer…why? Because terminating at null would result in valid paths being added twice: once for left null pointer and once for right null pointer!
Implementation (DFS)
There is also a great backtracking solution for further optimization. Basically in the above solution we are propagating a new copy of the path at every node. This is very inefficient from a memory complexity standpoint…and also time because a copying a list is an o(n)
operation.
We can use back-tracking to optimize this recursion and keep the current path and total in global variables. This is possible because of the nature of DFS traversals…when we reach a leaf, the path gets popped off until an unexplored path is reached:
Essentially the DFS traversal “climbs back” up the path it came before exploring the new leftmost path. Backtracking leverages this property to save resources.
Implementation (Backtracking)
Review 1
Really like the backtracking approach. I think I had a breakthrough when I originally did this problem, because i’m using this approach everywhere I can now. It’s just so much better than propagating the state.