forked from shuboc/LeetCode-2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsum-of-distances-in-tree.py
61 lines (53 loc) · 1.69 KB
/
sum-of-distances-in-tree.py
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
59
60
61
# Time: O(n)
# Space: O(n)
# An undirected, connected tree with N nodes
# labelled 0...N-1 and N-1 edges are given.
#
# The ith edge connects nodes edges[i][0] and edges[i][1] together.
#
# Return a list ans, where ans[i] is the sum of the distances
# between node i and all other nodes.
#
# Example 1:
#
# Input: N = 6, edges = [[0,1],[0,2],[2,3],[2,4],[2,5]]
# Output: [8,12,6,10,10,10]
# Explanation:
# Here is a diagram of the given tree:
# 0
# / \
# 1 2
# /|\
# 3 4 5
# We can see that dist(0,1) + dist(0,2) + dist(0,3) + dist(0,4) + dist(0,5)
# equals 1 + 1 + 2 + 2 + 2 = 8. Hence, answer[0] = 8, and so on.
# Note: 1 <= N <= 10000
import collections
class Solution(object):
def sumOfDistancesInTree(self, N, edges):
"""
:type N: int
:type edges: List[List[int]]
:rtype: List[int]
"""
def dfs(graph, node, parent, count, result):
for nei in graph[node]:
if nei != parent:
dfs(graph, nei, node, count, result)
count[node] += count[nei]
result[node] += result[nei]+count[nei]
def dfs2(graph, node, parent, count, result):
for nei in graph[node]:
if nei != parent:
result[nei] = result[node]-count[nei] + \
len(count)-count[nei]
dfs2(graph, nei, node, count, result)
graph = collections.defaultdict(list)
for u, v in edges:
graph[u].append(v)
graph[v].append(u)
count = [1] * N
result = [0] * N
dfs(graph, 0, None, count, result)
dfs2(graph, 0, None, count, result)
return result