Find Weighted Median Node in Tree - Problem

Imagine you're navigating through a network of interconnected cities, where each road has a specific travel cost. Your task is to find the weighted median node along any path between two cities.

You are given an integer n representing the number of nodes (0 to n-1) in an undirected, weighted tree rooted at node 0. The tree structure is defined by a 2D array edges where edges[i] = [u_i, v_i, w_i] represents a bidirectional edge between nodes u_i and v_i with weight w_i.

The weighted median node is the first node x on the path from node u to node v where the cumulative sum of edge weights from u to x is greater than or equal to half of the total path weight.

For each query [u_j, v_j], determine the weighted median node along the path from u_j to v_j. Return an array where each element corresponds to the weighted median node for the respective query.

Input & Output

example_1.py โ€” Basic Tree Query
$ Input: n = 5, edges = [[0,1,4],[1,2,6],[1,3,3],[1,4,5]], queries = [[3,4]]
โ€บ Output: [1]
๐Ÿ’ก Note: For the path from node 3 to node 4: 3 โ†’ 1 โ†’ 4. The edge weights are [3, 5] with total weight 8. The cumulative sum reaches 3 at node 1, and 3 + 5 = 8 at node 4. Since 8/2 = 4, and cumulative weight 8 โ‰ฅ 4, the weighted median is node 4. However, since we want the first node where cumulative โ‰ฅ half, we need to check: at node 1, cumulative is 3 < 4, but at the next edge (1โ†’4), cumulative becomes 8 โ‰ฅ 4, so the weighted median is node 4.
example_2.py โ€” Multiple Queries
$ Input: n = 4, edges = [[0,1,2],[1,2,3],[2,3,1]], queries = [[0,3],[1,3]]
โ€บ Output: [2,2]
๐Ÿ’ก Note: For query [0,3]: Path 0โ†’1โ†’2โ†’3, weights [2,3,1], total=6, target=3. Cumulative: 2 at node 1, 5 at node 2 (โ‰ฅ3), so median is node 2. For query [1,3]: Path 1โ†’2โ†’3, weights [3,1], total=4, target=2. Cumulative: 3 at node 2 (โ‰ฅ2), so median is node 2.
example_3.py โ€” Single Node Path
$ Input: n = 3, edges = [[0,1,5],[1,2,3]], queries = [[1,1]]
โ€บ Output: [1]
๐Ÿ’ก Note: When source and destination are the same node, the weighted median is the node itself.

Constraints

  • 2 โ‰ค n โ‰ค 104
  • edges.length == n - 1
  • 0 โ‰ค ui, vi < n
  • 1 โ‰ค wi โ‰ค 106
  • 1 โ‰ค queries.length โ‰ค 104
  • The graph forms a valid tree (connected and acyclic)

Visualization

Tap to expand
01234w=4w=6w=3w=5Query Path: 3 โ†’ 1 โ†’ 4Path AnalysisEdge weights: [3, 5]Total weight: 8Target: 8/2 = 4Median: Node 1
Understanding the Visualization
1
Build Tree Structure
Create adjacency list representation of the weighted tree
2
Find Path Between Nodes
Use tree traversal or LCA to find the unique path between query nodes
3
Calculate Total Weight
Sum all edge weights along the path to get total cost
4
Find Weighted Median
Traverse path accumulating weights until reaching โ‰ฅ half of total weight
Key Takeaway
๐ŸŽฏ Key Insight: In a tree, there's exactly one path between any two nodes, so we can efficiently find weighted medians using LCA preprocessing and path reconstruction.
Asked in
Google 38 Amazon 31 Meta 24 Microsoft 19
43.7K Views
Medium Frequency
~25 min Avg. Time
1.8K Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen