Junior — Senior
Finding the maximum path sum in a binary tree with a single visit to nodes
livecode
Task condition
Implement a function that calculates the maximum sum of nodes along any path, with the condition that each node can be visited no more than once. The path can start and end at any vertices of the tree — not necessarily from the root to a leaf. In the example below, the maximum sum is achieved by the path 5 → -3 → 20, which gives 30.
(-10)
/
(9) (20)
/ \
(-3) (8)
/ / \
(5) (-4) (-2)
/
(-2)
function getMaxPathSum(root) {}