Follow the parent arrows
Warm-up · Predict the result · about 3 min
The parent array of a union-find is A:A, B:A, C:B, D:B, E:A, F:D. How many parent arrows does find(F) follow before it reaches the root? Answer with a number.
Example 1
Input:
{"parent":{"A":"A","B":"A","C":"B","D":"B","E":"A","F":"D"}}
Output:
3
Constraints
- Answer with a number
- The root is the item whose parent is itself
Learn the idea first: Disjoint set union: find, union, rank, path compression, a visual lesson.
More Disjoint set union: find, union, rank, path compression problems
- Union sequence Warm-up
- Implement DSU Standard
- Number of components Standard
- Spot the bug: union without roots Standard
- Implement with union by size and compression Challenge