Problem
Submissions
Debug

Problem: Sum of Distances in a Tree

Statement

Given a connected, undirected tree with n nodes, labeled from 00 to n−1n - 1, and n−1n - 1 edges. The edges are provided in an array, where each element edges[i] =[ai,bi]= [a_i, b_i] represents an edge between nodes aia_i and bib_i.

Your task is to return an array ans of length n, where ans[i] is the total sum of the distances between the ithi_{th} node and all other nodes in the tree.

Constraints:

  • 1<=1 <= n <=3∗104<= 3 * 10^4

  • edges.length ==== n −1- 1

  • edges[i].length ==2== 2

  • 0<=ai,bi<0 <= a_i, b_i < n

  • ai!=bia_i != b_i

  • The given input represents a valid tree.

Problem
Submissions
Debug

Problem: Sum of Distances in a Tree

Statement

Given a connected, undirected tree with n nodes, labeled from 00 to n−1n - 1, and n−1n - 1 edges. The edges are provided in an array, where each element edges[i] =[ai,bi]= [a_i, b_i] represents an edge between nodes aia_i and bib_i.

Your task is to return an array ans of length n, where ans[i] is the total sum of the distances between the ithi_{th} node and all other nodes in the tree.

Constraints:

  • 1<=1 <= n <=3∗104<= 3 * 10^4

  • edges.length ==== n −1- 1

  • edges[i].length ==2== 2

  • 0<=ai,bi<0 <= a_i, b_i < n

  • ai!=bia_i != b_i

  • The given input represents a valid tree.