【LeetCode]ツリーツリー距離の距離の834合計と



無向、接続されたツリー  N ノードを標識  0...N-1 と  N-1 edges 与えられます。

i番目のエッジは、ノード接続  edges[i][0]  edges[i][1] 一緒にします。

リスト戻り  ans、  ans[i] ノード間の距離の合計であり  i 、他のすべてのノード。

例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.



Githubの同期住所:

https://github.com/grandyang/leetcode/issues/834



同様のトピック:
コインを配布するには、バイナリツリーであります



参考文献:

https://leetcode.com/problems/sum-of-distances-in-tree/



オールインワントピックのLeetCodeは(...更新)概要を説明します

おすすめ

転載: www.cnblogs.com/grandyang/p/11520804.html