-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution834.cs
58 lines (51 loc) · 1.67 KB
/
Solution834.cs
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
using System.Text;
using LeetCode.DataStructures;
namespace LeetCode.Solutions;
public class Solution834
{
/// <summary>
/// 834. Sum of Distances in Tree - Hard
/// <a href="https://leetcode.com/problems/sum-of-distances-in-tree">See the problem</a>
/// </summary>
public int[] SumOfDistancesInTree(int n, int[][] edges)
{
// Build adjacency list
var graph = new List<int>[n];
for (var i = 0; i < n; i++)
{
graph[i] = [];
}
foreach (var edge in edges)
{
graph[edge[0]].Add(edge[1]);
graph[edge[1]].Add(edge[0]);
}
var count = new int[n]; // Number of nodes in the subtree of each node
var answer = new int[n]; // Sum of distances for each node
// First DFS: Compute count and answer[0]
void FirstDFS(int node, int parent)
{
count[node] = 1; // Count itself
foreach (var neighbor in graph[node])
{
if (neighbor == parent) continue; // Skip parent
FirstDFS(neighbor, node);
count[node] += count[neighbor];
answer[0] += answer[neighbor] + count[neighbor];
}
}
// Second DFS: Propagate the result
void SecondDFS(int node, int parent)
{
foreach (var neighbor in graph[node])
{
if (neighbor == parent) continue; // Skip parent
answer[neighbor] = answer[node] + (n - 2 * count[neighbor]);
SecondDFS(neighbor, node);
}
}
FirstDFS(0, -1);
SecondDFS(0, -1);
return answer;
}
}