This problem requires us to point the next of the left node in each layer to the right node. Because it is processed layer by layer, it is very intuitive to think of using BFS to solve it.
After reading the problem at the beginning, you can probably feel that DFS should be used to solve it. Use DFS to calculate the sum of each inform time from headID to a leaf, and finally take the maximum time.
Union Find data structure is also known as disjoint set data structure. It is a data structure which stores a collection of disjoint sets. It provides two operations, one for merging two sets and one for finding the set with a given element.
Depth-first search (DFS) algorithm is a graph search algorithm. Starting from a given vertex, it first explores an adjacent vertex of that vertex. Next, it explores an adjacent vertex of this vertex. In this way, the exploration continues until a certain vertex has no adjacent vertices, it will return to the previous vertex, and explores the next adjacent vertex of the vertex. It will continue to explore like this until it finds a result, or explores all vertices.
Breadth-first search (BFS) algorithm is a graph search algorithm. Starting from a given vertex, it first explores all adjacent vertices of that vertex. Next, explores the adjacent vertices of these adjacent vertices in sequence. In this way, it explores level by level until the result is found, or all vertices are explored.
Swift 5.5 introduces Swift concurrency. It allows us to write asynchronous code in synchronous mechanism. Greatly reduce the complexity of asynchronous code.