831. Find the Last Marked Nodes in Tree
We are given an undirected tree with n nodes labeled from 0 to n - 1. The tree is specified by an array edges where each element [u, v] indicates an edge between nodes u and v.
Initially, no node is marked. At time t = 0, you choose one node to mark.
Every subsequent second, all unmarked nodes adjacent to any marked node become marked.
The process stops when all nodes are marked.
For each starting node i, determine which node is the last to be marked (if there are several candidates, any one is acceptable).
Example 1:
Input: edges = [[0,1]]
Output: [1,0]
Explanation:
Starting at 0: Mark 0 ā then 1 is marked. Last marked is 1.
Starting at 1: Mark 1 ā then 0 is marked. Last marked is 0.
Example 2:
Input: edges = [[0,1],[1,2],[2,3]]
Output: [3,3,0,0]
Explanation:
Starting at 0: Marks progress as 0 ā 1 ā 2 ā 3. Last is 3.
Starting at 1: Marks progress as 1 ā (0,2) ā 3. Last is 3.
Starting at 2: Marks progress as 2 ā (1,3) ā 0. Last is 0.
Starting at 3: Marks progress as 3 ā 2 ā 1 ā 0. Last is 0.
Now Your Turn!
Pick the correct output for the given inputInput: edges = [[0,1],[0,2],[2,3],[2,4]]
Still unsure what the problem is asking ?
Letās go through a few more examples, step by step, to make it clearer.
Constraints:
- 2 ⤠n ⤠10āµ
- edges.length == n - 1
- edges[i].length == 2
- 0 ⤠edges[i][0], edges[i][1] ⤠n - 1
- The given edges form a valid tree.