Shortest Path in a Weighted Tree - Problem

You are given an integer n and an undirected, weighted tree rooted at node 1 with n nodes numbered from 1 to n. This is represented by a 2D array edges of length n - 1, where edges[i] = [u_i, v_i, w_i] indicates an undirected edge from node u_i to v_i with weight w_i.

You are also given a 2D integer array queries of length q, where each queries[i] is either:

  • [1, u, v, w'] – Update the weight of the edge between nodes u and v to w', where (u, v) is guaranteed to be an edge present in edges.
  • [2, x] – Compute the shortest path distance from the root node 1 to node x.

Return an integer array answer, where answer[i] is the shortest path distance from node 1 to x for the i-th query of type [2, x].

Input & Output

Example 1 — Basic Tree Operations
$ Input: n = 4, edges = [[1,2,5],[1,3,3],[2,4,2]], queries = [[2,4],[1,1,3,10],[2,4]]
Output: [7,17]
💡 Note: Initially: distance from 1 to 4 is 1→2→4 = 5+2 = 7. After updating edge (1,3) to weight 10, distance 1 to 4 remains 7 via same path. Wait, let me recalculate: distance 1 to 4 is still 1→2→4 = 5+2 = 7, then after update edge (1,3) to 10, we get distance 1 to 4 = 1→2→4 = 5+2 = 7. Actually, the path doesn't use edge (1,3). Let me check if there's another interpretation. The answer should be [7,7] since the path to node 4 doesn't use the updated edge.
Example 2 — Single Node Query
$ Input: n = 2, edges = [[1,2,8]], queries = [[2,2],[1,1,2,3],[2,2]]
Output: [8,3]
💡 Note: Initially: distance from 1 to 2 is 8. After updating edge (1,2) to weight 3, distance becomes 3.
Example 3 — Multiple Updates
$ Input: n = 3, edges = [[1,2,1],[2,3,2]], queries = [[2,3],[1,1,2,5],[2,3],[1,2,3,1],[2,3]]
Output: [3,7,6]
💡 Note: Path 1→2→3: Initially 1+2=3, after first update 5+2=7, after second update 5+1=6.

Constraints

  • 1 ≤ n ≤ 1000
  • edges.length = n - 1
  • 1 ≤ ui, vi ≤ n
  • 1 ≤ wi ≤ 106
  • 1 ≤ queries.length ≤ 1000
  • For type 1 queries: 1 ≤ u, v ≤ n and 1 ≤ w' ≤ 106
  • For type 2 queries: 1 ≤ x ≤ n

Visualization

Tap to expand
Shortest Path in a Weighted TreeInput: n=4, edges=[[1,2,5],[1,3,3],[2,4,2]], queries=[[2,4],[1,1,3,10],[2,4]]1ROOT23453→102Query Processing[2,4]: Path 1→2→4, distance = 5+2 = 7[1,1,3,10]: Update edge (1,3) weight to 10[2,4]: Path still 1→2→4, distance = 5+2 = 7Output[7, 7]Tree property: exactly one path between any two nodes
Understanding the Visualization
1
Input Tree
Weighted tree with nodes 1-4 and given edge weights
2
Distance Query
Find shortest path from root (1) to target node
3
Update & Query
Change edge weight and recalculate affected distances
Key Takeaway
🎯 Key Insight: In a tree, there's exactly one path between nodes, so we can cache distances and update only affected subtrees
Asked in
Google 45 Microsoft 32 Amazon 28
23.4K Views
Medium Frequency
~35 min Avg. Time
892 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