869. Sum of Nodes on Longest Path Root to Leaf

EasyTreesBinary TreeDFSRecursion

Given the root of a binary tree, find the root-to-leaf path with the most nodes and return the sum of values along it. If two paths tie for the most nodes, return the larger sum. An empty tree has sum 0. The tree is given as a level-order array.

Input: A level-order array of the tree.

Output: Integer — the sum along the longest root-to-leaf path.

Examples

Example 1
Input: [1,2,3,4,5,null,6,7]
Output: 14
Explanation: Longest path 1-2-4-7 sums to 14.
Example 2
Input: [1]
Output: 1
Explanation: Single node.
Example 3
Input: []
Output: 0
Explanation: Empty tree.

Constraints

Asked by

TCSInfosysWiproCognizantCapgemini
Solve this problem in the editor →