Binary Tree Maximum Path Sum

Asked atQualcommDoorDashMicrosoftNutanixOracle
1Give yourself 5 minutes
2Answer out loud, not in your head
3Then compare with the answer below
0
Stuck? Show a way to structure it+
  1. 01Define DFS(node) as the best downward path gain that can be extended by the parent.
  2. 02Clamp each child gain at zero because a negative branch should be excluded.
  3. 03Use both child gains to update the best complete path passing through the current node.
  4. 04Return the node value plus only the better child gain to preserve a single path.

Reference answer

Then expect these follow-ups

  • How would you also reconstruct the nodes on the maximum path?

    Tests: state reconstruction

  • Why can the value returned to the parent include only one child?

    Tests: correctness reasoning

Free to read · better with Enzo

Practice this out loud with Enzo

Enzo runs it as a mock interview, pushes back with follow-ups, and grades you on the rubric.

Next question