Given a connected, undirected tree with n
nodes, labeled from 0 to n−1, and n−1 edges. The edges are provided in an array, where each element edges[i]
=[ai​,bi​] represents an edge between nodes ai​ and bi​.
Your task is to return an array ans
of length n
, where ans[i]
is the total sum of the distances between the ith​ node and all other nodes in the tree.
Constraints:
1<= n
<=3∗104
edges.length
== n
−1
edges[i].length
==2
0<=ai​,bi​< n
ai​!=bi​
The given input represents a valid tree.