Number of Good Paths - Problem

Imagine exploring a magical tree network where each node has a special value! You're given a tree (a connected graph with no cycles) consisting of n nodes numbered from 0 to n-1, along with their values and connections.

Your mission is to find all the "good paths" in this tree. A good path is a journey between two nodes that follows these magical rules:

  • ๐ŸŽฏ Same endpoints: The starting and ending nodes must have the same value
  • ๐Ÿ“ˆ Non-increasing journey: All nodes along the path must have values โ‰ค the endpoint values

For example, if you have a path A โ†’ B โ†’ C where A and C have value 3, then B must have value โ‰ค 3 for this to be a good path.

Note: Each single node counts as a good path by itself, and paths are undirected (Aโ†’B is the same as Bโ†’A).

Input & Output

example_1.py โ€” Simple Tree
$ Input: vals = [1,3,2,1,3], edges = [[0,1],[0,2],[2,3],[2,4]]
โ€บ Output: 6
๐Ÿ’ก Note: Tree structure: 1-3-2-1, with 2 connected to 3. Good paths are: (0)โ†’(0), (1)โ†’(1), (2)โ†’(2), (3)โ†’(3), (4)โ†’(4), (0)โ†’(3) [both have value 1]. Total = 6 paths.
example_2.py โ€” Linear Chain
$ Input: vals = [1,1,2,2,3], edges = [[0,1],[1,2],[2,3],[3,4]]
โ€บ Output: 7
๐Ÿ’ก Note: Linear chain: 1-1-2-2-3. Good paths: 5 single nodes + (0)โ†’(1) [value 1] + (2)โ†’(3) [value 2] = 7 total paths.
example_3.py โ€” Single Node
$ Input: vals = [1], edges = []
โ€บ Output: 1
๐Ÿ’ก Note: Only one node exists, so there's exactly 1 good path: the single node (0)โ†’(0).

Visualization

Tap to expand
0elev=13elev=11elev=32elev=34elev=3Mountain Trail NetworkElevation 1: 3 good trailsElevation 3: 6 good trailsTotal: 3 + 6 = 9 good trails
Understanding the Visualization
1
Map Elevations
Sort all checkpoints by elevation: [1,1,3,3,3] for nodes [0,3,1,2,4]
2
Process Lowest First
Start with elevation 1: connect nodes 0 and 3 through valid trails
3
Add Higher Elevations
Process elevation 3: connect nodes 1,2,4 and merge with existing network
4
Count Trail Networks
Count good paths in each connected component: C(2,2)+2=3 for elevation 1, C(3,2)+3=6 for elevation 3
Key Takeaway
๐ŸŽฏ Key Insight: By processing elevations from lowest to highest, we guarantee that when we connect trails at elevation V, all previously connected trails are at elevation โ‰ค V, ensuring valid good paths!

Time & Space Complexity

Time Complexity
โฑ๏ธ
O(nยณ)

O(nยฒ) pairs to check times O(n) DFS for each pair

n
2n
โš  Quadratic Growth
Space Complexity
O(n)

Space for recursion stack and grouping nodes by values

n
2n
โšก Linearithmic Space

Constraints

  • n == vals.length
  • 1 โ‰ค n โ‰ค 3 ร— 104
  • 0 โ‰ค vals[i] โ‰ค 105
  • edges.length == n - 1
  • edges[i].length == 2
  • 0 โ‰ค ai, bi < n
  • The input represents a valid tree
Asked in
Google 45 Amazon 38 Meta 32 Microsoft 28
48.2K Views
Medium-High Frequency
~25 min Avg. Time
1.9K 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