Run DFS from root. For each node:
Compute .
Compute .
Update .
Return to parent.
After DFS, return . The global variable accumulates the best answer seen at any node.
This runs in time and uses space.
##### ###### ##### ### # # ### # # ###### ## ## ## ## ## ## ## # # # # # ## ##### #### ##### # # # # # # # #### ## # ## ## ## ## # # # # # ## ## # ###### ## ### # ### # ######
##### ###### ##### ### # # ### # # ###### ## ## ## ## ## ## ## # # # # # ## ##### #### ##### # # # # # # # #### ## # ## ## ## ## # # # # # ## ## # ###### ## ### # ### # ######
DFS with global max
Run DFS from root. For each node:
Compute .
Compute .
Update .
Return to parent.
After DFS, return . The global variable accumulates the best answer seen at any node.
This runs in time and uses space.