树中的路径和 Sum of Distances in Tree
2019-03-28 15:25:43
问题描述:
问题求解:
写过的最好的Hard题之一。
初看本题,很经典的路径和嘛,dfs一遍肯定可以得到某个节点到其他所有节点的距离和。这种算法的时间复杂度是O(n ^ 2)。看一下数据量,emmm,果然不行。这个数据量一看就知道只能是O(n)的算法了。
只遍历一遍最多只能得到一个解,因此本题肯定是需要遍历至少两遍的。
在第一遍遍历的时候我们需要保存下两个值,一个是当前节点的subtree的路径总和,一个是当前节点的subtree的总的节点数。
在第二遍遍历的时候,我们已经知道root节点的值已经是最终的结果了,这个时候当我们将根节点从root移动到其child的时候,有cnt[child]的节点数的离现在的根节点近了一步,有N - cnt[child]的节点到当前根节点远了一步,所以res[child] = res[root] - cnt[child] + N - cnt[child]。这样再次遍历所有节点更新res数组得到的结果就是我们需要的最终的答案。
public int[] sumOfDistancesInTree(int N, int[][] edges) {
List<Set<Integer>> graph = new ArrayList<>();
int[] res = new int[N];
int[] cnt = new int[N];
for (int i = 0; i < N; i++) graph.add(new HashSet<>());
for (int[] edge : edges) {
graph.get(edge[0]).add(edge[1]);
graph.get(edge[1]).add(edge[0]);
}
dfs1(0, -1, res, cnt, graph);
dfs2(0, -1, res, cnt, graph);
return res;
} private void dfs1(int root, int parent, int[] res, int[] cnt, List<Set<Integer>> graph) {
for (int node : graph.get(root)) {
if (node == parent) continue;
dfs1(node, root, res, cnt, graph);
cnt[root] += cnt[node];
res[root] += res[node] + cnt[node];
}
cnt[root] += 1;
} private void dfs2(int root, int parent, int[] res, int[] cnt, List<Set<Integer>> graph) {
for (int node : graph.get(root)) {
if (node == parent) continue;
res[node] = res[root] - cnt[node] + cnt.length - cnt[node];
dfs2(node, root, res, cnt, graph);
}
}
2019-04-17 14:26:20
public int[] sumOfDistancesInTree(int N, int[][] edges) {
List<Set<Integer>> graph = new ArrayList<>();
for (int i = 0; i < N; i++) graph.add(new HashSet<>());
for (int[] edge : edges) {
graph.get(edge[0]).add(edge[1]);
graph.get(edge[1]).add(edge[0]);
}
int[] res = new int[N];
int[] cnt = new int[N];
int[] used = new int[N];
used[0] = 1;
dfs1(graph, 0, res, cnt, used);
Arrays.fill(used, 0);
used[0] = 1;
dfs2(graph, 0, res, cnt, used);
return res;
} private int[] dfs1(List<Set<Integer>> graph, int root, int[] res, int[] cnt, int[] used) {
res[root] = 0;
cnt[root] = 1;
for (int node : graph.get(root)) {
if (used[node] == 1) continue;
used[node] = 1;
int[] r = dfs1(graph, node, res, cnt, used);
res[root] += r[0] + r[1];
cnt[root] += r[1];
}
return new int[]{res[root], cnt[root]};
} private void dfs2(List<Set<Integer>> graph, int root, int[] res, int[] cnt, int[] used) {
for (int node : graph.get(root)) {
if (used[node] == 1) continue;
used[node] = 1;
res[node] = res[root] + cnt[0] - cnt[node] * 2;
dfs2(graph, node, res, cnt, used);
}
}
树中的路径和 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 a ...
- [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 co ...
- [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 co ...
- [LeetCode] 834. Sum of Distances in Tree
LeetCode刷题记录 传送门 Description An undirected, connected treewith N nodes labelled 0...N-1 and N-1 edge ...
- 【leetcode】834. Sum of Distances in Tree(图算法)
There is an undirected connected tree with n nodes labeled from 0 to n - 1 and n - 1 edges. You are ...
- leetcode834 Sum of Distances in Tree
思路: 树形dp. 实现: class Solution { public: void dfs(int root, int p, vector<vector<int>>& ...
- [LeetCode] 112. Path Sum ☆(二叉树是否有一条路径的sum等于给定的数)
Path Sum leetcode java 描述 Given a binary tree and a sum, determine if the tree has a root-to-leaf pa ...
- CodeChef Sum of distances(分治)
CodeChef Sum of distances(分治) 题目大意 有一排点,每个点 i 向 \(i + 1, i + 2, i + 3\) 分别连价值为 \(a_i,b_i,c_i\) 的有向边, ...
- Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all the values along the path equals the given sum. For example: Given the below binary tree andsum =
Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all ...
随机推荐
- ES6,ES5,ES3,对比学习~
在简书上看到一个博主写的文章,感觉很有用.留下:https://www.jianshu.com/p/287e0bb867ae Excuse me?这个前端面试在搞事!https://zhuanlan ...
- python打包
打包生成一个可执行文件(非窗口程序的) pyinstall -F example.py
- MongoDB在Windows系统下的安装和启动
版本选择MongoDB的版本命名规范如:x.y.z: y为奇数时表示当前版本为开发版,如:2.3.0.2.1.1: y为偶数时表示当前版本为稳定版,如:2.0.1.2.2.0: 目前官网上最新的版本为 ...
- php魔术变量以及命名空间
魔术变量: PHP 向它运行的任何脚本提供了大量的预定义常量. 不过很多常量都是由不同的扩展库定义的,只有在加载了这些扩展库时才会出现,或者动态加载后,或者在编译时已经包括进去了. 有八个魔术常量它们 ...
- ERROR 1698 (28000): Access denied for user 'root'@'localhost'
Some systems like Ubuntu, mysql is using by default the UNIX auth_socket plugin. Basically means tha ...
- 常见mysql的慢查询优化方式
一,第一步.开启mysql慢查询 方式一: 修改配置文件 在 my.ini 增加几行: 主要是慢查询的定义时间(超过2秒就是慢查询),以及慢查询log日志记录( slow_query_log) 方 ...
- java面试经常问到的计算机网络问题
GET 和 POST 的区别 GET请注意,查询字符串(名称/值对)是在 GET 请求的 URL 中发送的:/test/demo_form.asp?name1=value1&name2=val ...
- java消息服务学习之JMS概念
JMS即Java消息服务(Java Message Service)应用程序接口是一个Java平台中关于面向消息中间件(MOM)的API,用于在两个应用程序之间,或分布式系统中发送消息,进行异步通信. ...
- npm命令
简介:npm(node.js package manager)是Node.js的包管理器 .它创建于2009年,作为一个 开源项目,帮助开发人员轻松共享打包的代码模块 ## 默认方式初始化npm.(进 ...
- CSC 172 (Data Structures and Algorithms)
Project #3 (STREET MAPPING)CSC 172 (Data Structures and Algorithms), Spring 2019,University of Roche ...