#K48492. Maximum Path Sum in a Binary Tree
Maximum Path Sum in a Binary Tree
Maximum Path Sum in a Binary Tree
Given a binary tree represented as a nested list, where the tree is encoded in the following format:
[value, left_subtree, right_subtree]
Both left_subtree and right_subtree are either similarly structured lists or omitted. Your task is to calculate the maximum path sum in the tree.
A path is defined as any sequence of nodes in which each pair of adjacent nodes has a parent-child relationship. The path does not necessarily have to start at the root, and it may end at any node. The sum of a path is the sum of the node values along it.
You can formally define the maximum path sum at a node using the following formula: \[ \max( left_{single} + node,\; right_{single} + node,\; node,\; left_{single} + node + right_{single} ) \] where leftsingle and rightsingle respectively denote the maximum path sum starting from the left and right child nodes. The final answer is the maximum value over all nodes in the tree.
inputFormat
The input is provided as a single line in standard input that contains a JSON array representing the binary tree in the nested list format. For example:
[1, [2, [4], [5]], [3, [6], [7]]]
outputFormat
Output a single integer on standard output representing the maximum path sum of the provided binary tree.
## sample[1, [2, [4], [5]], [3, [6], [7]]]
18
</p>