-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsumofDistancesinTree.py
More file actions
42 lines (37 loc) · 1.41 KB
/
sumofDistancesinTree.py
File metadata and controls
42 lines (37 loc) · 1.41 KB
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
#! /usr/bin/env python3
# -*- coding: utf-8 -*-
# Source: https://leetcode.com/problems/sum-of-distances-in-tree/
# Author: Miao Zhang
# Date: 2021-03-16
###############################################################################
# res[A] = sum(A) + sum(B) + cnt(B)
# res[B] = sum(B) + sum(A) + cnt(A)
# res[A] - res[B] = cnt(B) - cnt(A)
# res[A] = res[B] - cnt(A) + N - cnt(A)
###############################################################################
class Solution:
def sumOfDistancesInTree(self, N: int, edges: List[List[int]]) -> List[int]:
graph = collections.defaultdict(set)
for u, v in edges:
graph[u].add(v)
graph[v].add(u)
count = [1] * N
res = [0] * N
def dfs(node = 0, parent = None):
'''
cnt: Number of nodes with node as root node
res: Distance of node as root node
'''
for child in graph[node]:
if child != parent:
dfs(child, node)
count[node] += count[child]
res[node] += res[child] + count[child]
def dfs2(node = 0, parent = None):
for child in graph[node]:
if child != parent:
res[child] = res[node] - count[child] + N - count[child]
dfs2(child, node)
dfs()
dfs2()
return res