[Swift]LeetCode834. 树中距离之和 | Sum of Distances in Tree

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

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

Return a list ans, where ans[i] is the sum of the distances between node i and all other nodes.

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

Note: 1 <= N <= 10000



给定一个无向、连通的树。树中有 N 个标记为 0...N-1 的节点以及 N-1 条边 。

第 i 条边连接节点 edges[i][0] 和 edges[i][1] 。

返回一个表示节点 i 与其他所有节点距离之和的列表 ans

示例 1:

输入: N = 6, edges = [[0,1],[0,2],[2,3],[2,4],[2,5]]
输出: [8,12,6,10,10,10]
解释:
如下为给定的树的示意图:
  0
 / 1   2
   /|  3 4 5

我们可以计算出 dist(0,1) + dist(0,2) + dist(0,3) + dist(0,4) + dist(0,5)
也就是 1 + 1 + 2 + 2 + 2 = 8。 因此,answer[0] = 8,以此类推。

说明: 1 <= N <= 10000



Runtime: 364 ms

Memory Usage: 20.3 MB

 1 class Solution {
 2     var res:[Int] = [Int]()
 3     var count:[Int] = [Int]()
 4     var tree:[Set<Int>] = [Set<Int>]()
 5     func sumOfDistancesInTree(_ N: Int, _ edges: [[Int]]) -> [Int] {
 6         res = [Int](repeating:0,count:N)
 7         count = [Int](repeating:0,count:N)
 8         tree = [Set<Int>](repeating:Set<Int>(),count:N)
 9         for e in edges
10         {
11             tree[e[0]].insert(e[1])
12             tree[e[1]].insert(e[0])
13         }
14         dfs(0, -1)
15         dfs2(0, -1)
16         return res
17     }
18
19     func dfs(_ root:Int,_ pre:Int)
20     {
21         for i in tree[root]
22         {
23             if i == pre {continue}
24             dfs(i, root)
25             count[root] += count[i]
26             res[root] += res[i] + count[i]
27         }
28         count[root] += 1
29     }
30
31     func dfs2(_ root:Int,_ pre:Int)
32     {
33         for i in tree[root]
34         {
35             if i == pre {continue}
36             res[i] = res[root] - count[i] + count.count - count[i]
37             dfs2(i, root)
38         }
39     }
40 }

原文地址:https://www.cnblogs.com/strengthen/p/10576111.html

时间: 2024-08-30 04:21:49

[Swift]LeetCode834. 树中距离之和 | Sum of Distances in Tree的相关文章

834. Sum of Distances in Tree —— weekly contest 84

Sum of Distances in Tree An undirected, connected tree with N nodes labelled 0...N-1 and N-1 edges are given. The ith edge connects nodes edges[i][0] and edges[i][1] together. Return a list ans, where ans[i] is the sum of the distances between node i

[LeetCode] 834. Sum of Distances in Tree 树中距离之和

An undirected, connected?tree with?N?nodes labelled?0...N-1?and?N-1?edges?are?given. The?ith edge connects nodes?edges[i][0]?and?edges[i][1]?together. Return a list?ans, where?ans[i]?is the sum of the distances between node?i?and all other nodes. Exa

[Swift]LeetCode404. 左叶子之和 | Sum of Left Leaves

Find the sum of all left leaves in a given binary tree. Example: 3 / 9 20 / 15 7 There are two left leaves in the binary tree, with values 9 and 15 respectively. Return 24. 计算给定二叉树的所有左叶子之和. 示例: 3 / 9 20 / 15 7 在这个二叉树中,有两个左叶子,分别是 9 和 15,所以返回 24 1 /**

[Swift]LeetCode371. 两整数之和 | Sum of Two Integers

Calculate the sum of two integers a and b, but you are not allowed to use the operator + and -. Example:Given a = 1 and b = 2, return 3. Credits:Special thanks to @fujiaozhu for adding this problem and creating all test cases. 不使用运算符 + 和 - ???????,计算

leetcode834 Sum of Distances in Tree

思路: 树形dp. 实现: 1 class Solution 2 { 3 public: 4 void dfs(int root, int p, vector<vector<int>>& G, vector<int>& cnt, vector<int>& res) 5 { 6 for (auto it: G[root]) 7 { 8 if (it == p) continue; 9 dfs(it, root, G, cnt, res)

51NOD 1110 距离之和最小 V3(中位数 + 技巧)

传送门 X轴上有N个点,每个点除了包括一个位置数据X[i],还包括一个权值W[i].该点到其他点的带权距离 = 实际距离 * 权值.求X轴上一点使它到这N个点的带权距离之和最小,输出这个最小的带权距离之和. Input 第1行:点的数量N.(2 <= N <= 10000) 第2 - N + 1行:每行2个数,中间用空格分隔,分别是点的位置及权值.(-10^5 <= X[i] <= 10^5,1 <= W[i] <= 10^5) Output 输出最小的带权距离之和.

1108 距离之和最小V2

1108 距离之和最小 V2 三维空间上有N个点, 求一个点使它到这N个点的曼哈顿距离之和最小,输出这个最小的距离之和. 点(x1,y1,z1)到(x2,y2,z2)的曼哈顿距离就是|x1-x2| + |y1-y2| + |z1-z2|.即3维坐标差的绝对值之和. Input 第1行:点的数量N.(2 <= N <= 10000) 第2 - N + 1行:每行3个整数,中间用空格分隔,表示点的位置.(-10^9 <= X[i], Y[i], Z[i] <= 10^9) Output

1110 距离之和最小 V3

1110 距离之和最小 V3 基准时间限制:1 秒 空间限制:131072 KB X轴上有N个点,每个点除了包括一个位置数据X[i],还包括一个权值W[i].该点到其他点的带权距离 = 实际距离 * 权值.求X轴上一点使它到这N个点的带权距离之和最小,输出这个最小的带权距离之和. Input 第1行:点的数量N.(2 <= N <= 10000) 第2 - N + 1行:每行2个数,中间用空格分隔,分别是点的位置及权值.(-10^5 <= X[i] <= 10^5,1 <= 

51Nod 1110 距离之和最小 V3 中位数 思维

基准时间限制:1 秒 空间限制:131072 KB 分值: 40 难度:4级算法题 X轴上有N个点,每个点除了包括一个位置数据X[i],还包括一个权值W[i].点P到点P[i]的带权距离 = 实际距离 * P[i]的权值.求X轴上一点使它到这N个点的带权距离之和最小,输出这个最小的带权距离之和.Input第1行:点的数量N.(2 <= N <= 10000)第2 - N + 1行:每行2个数,中间用空格分隔,分别是点的位置及权值.(-10^5 <= X[i] <= 10^5,1 &