PythiloHard · 40 min · Task 19 of 20 · progress kept in this browser

Best path in a tree

A tree is written as nested tuples, (value, left, right), with None for a missing child. Values may be negative.

A path is a connected chain of nodes, each used at most once. It need not pass through the root, but it cannot branch. Find the largest possible sum of a path with at least one node.

leaf = lambda v: (v, None, None)
best_path((1, leaf(2), leaf(3)))  # 6
Your task

Write best_path(tree) returning the largest sum of any path in a non-empty tree given as nested (value, left, right) tuples.