Best Sum Downward Tree Path
For a tree with n nodes rooted at node 0 (nodes numbered from 0 to n-1), where each node i has a value given by values[i], determine the maximum sum of values along any path that starts at a node u and only goes downward in the tree.
Consider only paths of the form u₁, u₂, u₃, ..., uₖ where each node uᵢ is a child of node uᵢ₋₁ for 1 < i ≤ k. For example, given the following tree (labeled node number/value):
Two possible paths are 0 → 1 → 2 → 3 which has a sum of 5 + 7 + (-10) + 4 = 6 and 1 → 2 → 3 with a sum of 7 + (-10) + 4 = 1. The highest sum path is 0 → 4 with a sum of 5 + 15 = 20.
Complete the function bestSumDownwardTreePath in the editor with the following parameter(s):
int parent[n]: eachparent[i]represents the parent node for nodei,parent[i] = -1means nodeiis the rootint values[n]: eachvalues[i]represents the value of nodei
Returns
int: the largest sum of values along a path down the tree from any node u
capacity = [9, 3, 3, 3, 9] return = 2
1 ≤ n ≤ 10⁵parent[0] = -10 ≤ parent[i] < n-1for0 < i < n-1-1000 ≤ values[i] ≤ 1000- the tree described is valid
- Minimum Path Sum to Target in Binary TreePHONE SCREEN · Seen Apr 2026
- Social Media SuggestionsSeen May 2025
- Price CheckSeen Feb 2025
- Palindromic Substrings (LC 647 :)Seen Jan 2025
- Get Distinct Goodness ValuesSeen Jan 2025
- Get Min OperationsSeen Jan 2025
- Count Stable SegmentsSeen Jan 2025
- Find Consistent LogsSeen Oct 2024
public int bestSumDownwardTreePath(int[] parent, int[] values) {
// write your code here
}