Number of Good Paths - Problem

There is a tree (i.e. a connected, undirected graph with no cycles) consisting of n nodes numbered from 0 to n - 1 and exactly n - 1 edges.

You are given a 0-indexed integer array vals of length n where vals[i] denotes the value of the ith node. You are also given a 2D integer array edges where edges[i] = [ai, bi] denotes that there exists an undirected edge connecting nodes ai and bi.

A good path is a simple path that satisfies the following conditions:

  • The starting node and the ending node have the same value.
  • All nodes between the starting node and the ending node have values less than or equal to the starting node (i.e. the starting node's value should be the maximum value along the path).

Return the number of distinct good paths.

Note: A path and its reverse are counted as the same path. For example, 0 → 1 is considered to be the same as 1 → 0. A single node is also considered as a valid path.

Input & Output

Example 1 — Basic Tree Structure
$ Input: vals = [1,3,2,1,3], edges = [[0,1],[0,2],[2,3],[2,4]]
Output: 7
💡 Note: Good paths are: single nodes (5) + path 0→2→3 (nodes 0,3 both have value 1) + path 1→2→4 (nodes 1,4 both have value 3) = 5 + 1 + 1 = 7 total paths
Example 2 — All Same Values
$ Input: vals = [1,1,2,1,1], edges = [[0,1],[1,2],[2,3],[3,4]]
Output: 10
💡 Note: Many good paths exist between nodes with value 1: all single nodes (5) + pairs of nodes with value 1 that can connect through node 2 (value 2) = 5 + 5 = 10 paths
Example 3 — Simple Path
$ Input: vals = [1], edges = []
Output: 1
💡 Note: Single node forms a valid good path by itself

Constraints

  • 1 ≤ n ≤ 3 * 104
  • 0 ≤ vals[i] ≤ 105
  • edges.length == n - 1
  • edges[i].length == 2
  • 0 ≤ ai, bi < n
  • ai ≠ bi
  • The given edges represent a valid tree

Visualization

Tap to expand
Number of Good Paths INPUT 1 n0 3 n1 2 n2 1 n3 3 n4 vals = [1,3,2,1,3] edges: [0,1],[0,2] [2,3],[2,4] Good Path: start=end value, all between <= start value ALGORITHM STEPS 1 Sort nodes by value Process: 1,1,2,3,3 2 Init Union-Find Each node is own parent 3 Process by value Union nodes, count pairs 4 Count good paths C(k,2) for k same vals Processing Order: val=1: nodes 0,3 val=2: node 2 (union) val=3: nodes 1,4 Single nodes: 5 paths Pairs (1-4, 1-3): 2 paths FINAL RESULT 7 Good Paths Found: Single Node Paths (5): 0-->0, 1-->1, 2-->2 3-->3, 4-->4 (each node to itself) Multi-Node Paths (2): 1-->0-->2-->4 (val=3) Path: node1 to node4 (both endpoints = 3) Output 7 OK - 5 + 2 = 7 distinct good paths Key Insight: By sorting nodes by value and using Union-Find, we process edges only when both endpoints have been visited. This ensures all intermediate nodes have smaller values. For each value, count pairs of same-value nodes in same component: C(k,2) = k*(k-1)/2 for k nodes. TutorialsPoint - Number of Good Paths | Union-Find with Sorting Approach
Asked in
Google 15 Facebook 12 Amazon 8
18.5K 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