A squirrel hops through the tree looking for the path of nodes that adds up to the biggest treasure. Watch how it decides, step by step!
๐ฌ
Press Play and watch the squirrel start its hunt!
Left branch gives
โ
Right branch gives
โ
Best treasure so far
โ
Sends up to parent
โ
Step 0 / 0
๐ข Slow
Fast ๐
Not visited yet On the leading path Winning treasure path Negative number (grumpy!)
๐งญ How the squirrel thinks
The squirrel visits every node, always checking its left friend and right friend first (that's called visiting children before yourself).
If a branch's best number is negative, the squirrel just says "no thanks!" and treats it as 0 โ no point dragging a grumpy number along.
At each node, the squirrel imagines a bridge path: left branch + this node + right branch, and checks if that beats the best treasure found anywhere so far.
But when reporting back up to its parent, the squirrel can only carry one branch (left OR right) โ a real path can't branch in two directions at once!