← 返回 bytedance 的题目列表Modified DFS Problem
类型:online_judge
Given a tree with n nodes, where each node has a weight. Find the maximum path sum from root to a leaf using Depth First Search (DFS). The tree is represented in adjacency list format.
Input:
n: Number of nodes in the tree
edges: A 2D list representing the edges in the tree, each edge as [u, v]
values: A list of weights for each node
Output:
The maximum path sum.
Example:
Input: n = 3, edges = [[0, 1], [0, 2]], values = [3, 1, 5]
Output: 8
Explanation: The path 0->2 gives the sum 3+5=8.
Example
Input
3
[[0, 1], [0, 2]]
[3, 1, 5]
Output
8