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,7]
💡 Note: Initially: distance from 1 to 4 is 1→2→4 = 5+2 = 7. After updating edge (1,3) to weight 10, the distance from 1 to 4 remains 7 because the shortest path 1→2→4 doesn't use the updated edge (1,3).
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 Tree INPUT 1 2 3 4 w=5 w=3 w=2 n = 4 edges: [[1,2,5],[1,3,3],[2,4,2]] queries: [2,4] - dist to node 4 [1,1,3,10] - update edge [2,4] - dist to node 4 ALGORITHM STEPS 1 Build Tree Store edges in adjacency list 2 Precompute Distances DFS from root, store dist[] 3 Process Queries Type 1: Update, Type 2: Query 4 Handle Updates Recalculate subtree distances Distance Calculation Query [2,4]: dist[4] = dist[1]+5+2 = 0+5+2 = 7 Update [1,1,3,10]: edge(1,3) weight: 3 --> 10 Query [2,4]: dist[4] = 0+5+2+10 = 17 (via 3) FINAL RESULT 1 d=0 2 d=5 3 d=10 4 d=7 Output: [7, 17] Query 1: dist[4] = 7 After update: dist[4] = 17 OK - Verified Key Insight: In a tree, there is exactly ONE path between any two nodes. Precompute distances from root using DFS. For updates, only the subtree rooted at the child node of the updated edge needs distance recalculation. TutorialsPoint - Shortest Path in a Weighted Tree | Precompute Distances with Updates
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