Find the Last Marked Nodes in Tree Problem
Description
LeetCode Problem 3313.
There exists an undirected tree with n nodes numbered 0 to n - 1. You are given a 2D integer array edges of length n - 1, where edges[i] = [u_i, v_i] indicates that there is an edge between nodes u_i and v_i in the tree.
Initially, all nodes are unmarked. After every second, you mark all unmarked nodes which have at least one marked node adjacent to them.
Return an array nodes where nodes[i] is the last node to get marked in the tree, if you mark node i at time t = 0. If nodes[i] has multiple answers for any node i, you can choose any one answer.
Example 1:
Input: edges = [[0,1],[0,2]]
Output: [2,2,1]
Explanation:

- For i = 0, the nodes are marked in the sequence: [0] -> [0,1,2]. Either 1 or 2 can be the answer.
- For i = 1, the nodes are marked in the sequence: [1] -> [0,1] -> [0,1,2]. Node 2 is marked last.
- For i = 2, the nodes are marked in the sequence: [2] -> [0,2] -> [0,1,2]. Node 1 is marked last.
Example 2:
Input: edges = [[0,1]]
Output: [1,0]
Explanation:

- For i = 0, the nodes are marked in the sequence: [0] -> [0,1].
- For i = 1, the nodes are marked in the sequence: [1] -> [0,1].
Example 3:
Input: edges = [[0,1],[0,2],[2,3],[2,4]]
Output: [3,3,1,1,1]
Explanation:

- For i = 0, the nodes are marked in the sequence: [0] -> [0,1,2] -> [0,1,2,3,4].
- For i = 1, the nodes are marked in the sequence: [1] -> [0,1] -> [0,1,2] -> [0,1,2,3,4].
- For i = 2, the nodes are marked in the sequence: [2] -> [0,2,3,4] -> [0,1,2,3,4].
- For i = 3, the nodes are marked in the sequence: [3] -> [2,3] -> [0,2,3,4] -> [0,1,2,3,4].
- For i = 4, the nodes are marked in the sequence: [4] -> [2,4] -> [0,2,3,4] -> [0,1,2,3,4].
Constraints:
2 <= n <= 10^5edges.length == n - 1edges[i].length == 20 <= edges[i][0], edges[i][1] <= n - 1- The input is generated such that
edgesrepresents a valid tree.
Solution
Approach: Diameter Endpoints + BFS
The node marked last when starting from i is a farthest node from i. In a tree, a farthest node from any node can always be chosen as one of the two endpoints of a diameter.
Find the diameter with three BFS runs: from any node, take the farthest node a; from a, take the farthest node b and record every distance to a as dist2; from b, record the distances dist3. For each node i, the answer is a if dist2[i] > dist3[i], otherwise b. BFS is used instead of recursion because the tree can be a path of 10^5 nodes.
Complexity: O(n) time and O(n) space, where n is the number of nodes.
class Solution:
def lastMarkedNodes(self, edges: List[List[int]]) -> List[int]:
def bfs(s: int) -> List[int]:
dist = [-1] * n
dist[s] = 0
q = [s]
for i in q:
for j in g[i]:
if dist[j] < 0:
dist[j] = dist[i] + 1
q.append(j)
return dist
n = len(edges) + 1
g = [[] for _ in range(n)]
for u, v in edges:
g[u].append(v)
g[v].append(u)
dist1 = bfs(0)
a = dist1.index(max(dist1))
dist2 = bfs(a)
b = dist2.index(max(dist2))
dist3 = bfs(b)
return [a if x > y else b for x, y in zip(dist2, dist3)]