forked from doocs/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution2.java
30 lines (28 loc) · 827 Bytes
/
Solution2.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
class Solution {
private Map<Integer, List<Integer>> g = new HashMap<>();
private int[] ans;
public int[] restoreArray(int[][] adjacentPairs) {
for (var e : adjacentPairs) {
int a = e[0], b = e[1];
g.computeIfAbsent(a, k -> new ArrayList<>()).add(b);
g.computeIfAbsent(b, k -> new ArrayList<>()).add(a);
}
int n = adjacentPairs.length + 1;
ans = new int[n];
for (var e : g.entrySet()) {
if (e.getValue().size() == 1) {
dfs(e.getKey(), 1000000, 0);
break;
}
}
return ans;
}
private void dfs(int i, int fa, int k) {
ans[k++] = i;
for (int j : g.get(i)) {
if (j != fa) {
dfs(j, i, k);
}
}
}
}