Quant Memo
Coding/●●●●●

Best-value path anywhere in a decision tree

Asked at Two Sigma

Each node of a binary tree carries a value (which can be negative). A path is any sequence of connected nodes that goes through each node at most once; it may start and end anywhere and may bend at a node (using both children). Find the maximum possible sum along any path.

        -10
        /  \
       9    20
           /  \
          15   7
-> 42       # 15 -> 20 -> 7

Return the maximum path sum. The best path need not touch the root.

Your answer

This one is open-ended. Work it through, then check your reasoning against the full solution.

More Coding questions