A recursive tree function often answers a smaller question than the final problem asks. A subtree height is a downward arm that a parent can extend. A maximum path can join two arms at their shared parent, but that joined shape can't itself be returned as one arm. Keep those two meanings separate.
You will begin by summing only leaves, then read a right-side view by breadth-first levels and find a ranked key by inorder traversal. The final tasks count downward target sums and maximize a signed route. For the path-count map, record only ancestors on the active branch and undo each addition when returning. These trees have a depth bound of 300; without such a bound, an iterative traversal avoids depending on the language call-stack limit.