An undirected, connected tree with **N** nodes labelled **0…N-1** and **N-1** edges are given.

The ith edge connects nodes **A[i][0]** and **A[i][1]** together.

Return an array of integers, **ith index** representes the sum of the distances between **node i** and all other nodes.

**Input Format:**

```
The first and the only argument has a 2-d array of integers of size N x 2, where nodes A[i][0] and A[i][1] are connected.
```

**Output Format:**

```
Return an array of integers, where the ith index represents the answer as described in the problem statement.
```

**Constraints**

```
1 <= A <= 10000
```

**For Example:**

```
Input 1:
A = [[3, 1], [2, 1], [0, 2], [3, 4]]
Output 1:
[10, 6, 7, 7, 10]
Explanation 1:
dist(0, 1) = 2
dist(0, 2) = 1
dist(0, 3) = 3
dist(0, 4) = 4
Total sum of distances for Node 0 : 1 + 2 + 3 + 4 = 10.
```

NOTE:You only need to implement the given function. Do not read input, instead use the arguments to the function. Do not print the output, instead return values as specified. Still have a doubt? Checkout Sample Codes for more details.

Sign Up

to access hints and editorial solutions for**Trees and Sum of Distances**

to access hints and editorial solutions for

Loading...