Skip to content

Practice Minimum Edge Weight Equilibrium Queries in a Tree

Read the task, work through the examples, and check your approach against the constraints. Use the original LeetCode task for coding and submissions.

Hard#2846
LeetCode Problem

Minimum Edge Weight Equilibrium Queries in a Tree

Problem Statement

The n-node tree has edges [u,v,w], with weight w on the undirected edge between u and v. For each query [a,b], find the fewest edge-weight changes needed to make every edge on the path from a to b have the same weight. A change may assign any weight to one edge. Queries are independent: each starts with the original weights. Return answers in query order.

Source: Minimum Edge Weight Equilibrium Queries in a Tree on LeetCode. Use the original task for its coding environment and submissions.

Examples

# Example 1

Input
n = 7, edges = [[0,1,1],[1,2,1],[2,3,1],[3,4,2],[4,5,2],[5,6,2]], queries = [[0,3],[3,6],[2,6],[0,6]]
Output
[0,0,1,3]

# Example 2

Input
n = 8, edges = [[1,2,6],[1,3,4],[2,4,6],[2,5,3],[3,6,6],[3,0,8],[7,0,2]], queries = [[4,6],[0,4],[6,5],[7,4]]
Output
[1,2,2,3]

Constraints

1 <= n <= 10^4
edges.length == n - 1
edges[i].length == 3
0 <= ui, vi < n
1 <= wi <= 26
The input is generated such that edges represents a valid tree.
1 <= queries.length == m <= 2 * 10^4
queries[i].length == 2
0 <= ai, bi < n

Related LeetCode Problems

Practice more problems similar to Minimum Edge Weight Equilibrium Queries in a Tree.

Review your practice with PhantomCodeAI

After trying the problem yourself, use the app to discuss your approach, complexity, and edge cases.