Loading content...
A route in a binary tree is defined as a continuous sequence of nodes where consecutive nodes in the sequence share a direct parent-child edge connection. Each node in the tree can participate in the route at most once — meaning the route cannot revisit or loop through any node. Importantly, the route does not have to traverse through the root node; it can begin and end at any nodes within the tree.
The route sum represents the cumulative total of all node values along the chosen route.
Given the root of a binary tree containing integer values (which may be positive, negative, or zero), your task is to determine and return the maximum route sum achievable by any valid non-empty route in the tree.
Key Observations:
root = [1,2,3]6The optimal route traverses through all three nodes: 2 → 1 → 3. The route sum is 2 + 1 + 3 = 6. This route uses the root as a bridge connecting the left and right children.
root = [-10,9,20,null,null,15,7]42The optimal route is 15 → 20 → 7, entirely within the right subtree. The route sum is 15 + 20 + 7 = 42. Notice that including the root (-10) would decrease the sum, so we exclude it from our route.
root = [5]5The tree contains only a single node with value 5. The only possible route is the node itself, giving a route sum of 5.
Constraints