Practice Find Weighted Median Node in Tree
Read the task, work through the examples, and check your approach against the constraints. Use the original LeetCode task for coding and submissions.
Find Weighted Median Node in Tree
Problem Statement
You are given an integer n and an undirected, weighted tree rooted at node 0 with n nodes numbered from 0 to n - 1. This is represented by a 2D array edges of length n - 1, where edges[i] = [ui, vi, wi] indicates an edge from node ui to vi with weight wi. The weighted median node is defined as the first node x on the path from ui to vi such that the sum of edge weights from ui to x is greater than or equal to half of the total path weight. You are given a 2D integer array queries. For each queries[j] = [uj, vj], determine the weighted median node along the path from uj to vj. Return an array ans, where ans[j] is the node index of the weighted median for queries[j].
Source: Find Weighted Median Node in Tree on LeetCode. Use the original task for its coding environment and submissions.
Examples
# Example 1
n = 2, edges = [[0,1,7]], queries = [[1,0],[0,1]]
[0,1]
# Example 2
n = 3, edges = [[0,1,2],[2,0,4]], queries = [[0,1],[2,0],[1,2]]
[1,0,2]
# Example 3
n = 5, edges = [[0,1,2],[0,2,5],[1,3,1],[2,4,3]], queries = [[3,4],[1,2]]
[2,2]
Constraints
Related LeetCode Problems
Practice more problems similar to Find Weighted Median Node in Tree.
Review your practice with PhantomCodeAI
After trying the problem yourself, use the app to discuss your approach, complexity, and edge cases.