【HackerRank】Cut the tree
题目链接:Cut the tree
题解:题目要求求一条边,去掉这条边后得到的两棵树的节点和差的绝对值最小。
暴力求解会超时。
如果我们可以求出以每个节点为根的子树的节点之和,那么当我们去掉一条边(a,b)的时候,其中的一棵树必是以a或者b为根的子树,那么我们就可以知道生成的两棵树的节点之和了。所以,当我们得到以每个节点为根的子树节点和这个信息后(把这个信息存储在TreeNode的value变量中),通过遍历每个节点(每个节点为根的子树必然为某次cut得到的子树之一,例如下图中,2为根的子树对应切割边(1,2)得到的树,而5为根的子树对应切割边(1,5)得到的树),求出最小的W-2*root.value即为所求(W是所有节点的值得和)。

树节点的数据结构定义如下:
static class TreeNode{
int value;
int number;
ArrayList<TreeNode> children;
public TreeNode(int value,int number){
this.value = value;
this.number = number;
children = new ArrayList<TreeNode>();
}
}
value表示该顶点对应的值,number表示顶点的编号,children表示子节点的列表(这里指表示连接关系,也有可能children里面其实存放了父节点,不过可以用接下来的visited数组避免访问到父节点)。
然后从树的任意一个节点开始dfs,把它的value值依次加上各个child为root的子树节点值,就得到以它为root的子树的节点值得和了。而它的child为root的子树节点和就用递归的方法求。上面说了,每次读入一条边(a,b)的时候,即把b加入到a的children集合里面,也把a加入到b的children集合里面。那么在dfs的时候,怎么知道谁是父节点,谁是子节点呢?就用一个visited数组,因为在dfs过程中,父节点一定先被访问,所以在一个节点的children集合里面,已经被visited过的节点就不是它的子节点了。
最后代码如下:
import java.util.*;
public class Solution {
static class TreeNode{
int value;
int number;
ArrayList<TreeNode> children;
public TreeNode(int value,int number){
this.value = value;
this.number = number;
children = new ArrayList<TreeNode>();
}
}
private static int dfs(TreeNode root,boolean[] visited){
visited[root.number]=true;
for(TreeNode child:root.children){
if(!visited[child.number]){
root.value += dfs(child, visited);
}
}
return root.value;
}
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n = in.nextInt();
TreeNode[] treeNodes = new TreeNode[n];
int W = 0;
for(int i = 0;i < n;i++)
{
int value = in.nextInt();
treeNodes[i]=new TreeNode(value,i);
W += value;
}
for(int i = 0;i < n-1;i++){
int a = in.nextInt();
int b = in.nextInt();
treeNodes[a-1].children.add(treeNodes[b-1]);
treeNodes[b-1].children.add(treeNodes[a-1]);
}
boolean[] visited = new boolean[n];
dfs(treeNodes[0], visited);
int miniDif = Integer.MAX_VALUE;
for(TreeNode t:treeNodes){
miniDif = Math.min(miniDif, Math.abs(W-2*t.value));
}
System.out.println(miniDif);
}
}
另外要积累的一点是java中nested class和inner class的区别,见stackoverflow上面的详细解答,所以上述代码中第4行要把TreeNode声明为nested class,否则它只能通过类Solution调用。
【HackerRank】Cut the tree的更多相关文章
- 【数据结构】B-Tree, B+Tree, B*树介绍 转
[数据结构]B-Tree, B+Tree, B*树介绍 [摘要] 最近在看Mysql的存储引擎中索引的优化,神马是索引,支持啥索引.全是浮云,目前Mysql的MyISAM和InnoDB都支持B-Tre ...
- 【题解】Cut the Sequence(贪心区间覆盖)
[题解]Cut the Sequence(贪心区间覆盖) POJ - 3017 题意: 给定一大堆线段,问用这些线段覆盖一个连续区间1-x的最小使用线段的数量. 题解 考虑一个这样的贪心: 先按照左端 ...
- 【LeetCode】199. Binary Tree Right Side View 解题报告(Python)
[LeetCode]199. Binary Tree Right Side View 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/probl ...
- 【数据结构】B-Tree, B+Tree, B*树介绍
[摘要] 最近在看Mysql的存储引擎中索引的优化,神马是索引,支持啥索引.全是浮云,目前Mysql的MyISAM和InnoDB都支持B-Tree索引,InnoDB还支持B+Tree索引,Memory ...
- 【LeetCode】Balanced Binary Tree 解题报告
[题目] Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced bi ...
- 【LeetCode】145. Binary Tree Postorder Traversal
Difficulty: Hard More:[目录]LeetCode Java实现 Description https://leetcode.com/problems/binary-tree-pos ...
- 【HackerRank】Utopian tree
The Utopian tree goes through 2 cycles of growth every year. The first growth cycle of the tree occu ...
- 【BZOJ-4353】Play with tree 树链剖分
4353: Play with tree Time Limit: 20 Sec Memory Limit: 256 MBSubmit: 31 Solved: 19[Submit][Status][ ...
- 【leetcode】Binary Search Tree Iterator(middle)
Implement an iterator over a binary search tree (BST). Your iterator will be initialized with the ro ...
随机推荐
- Groovy基本类型与运算符
字符串 1.1字符串段落 def s = """Groovy Grails JAVA """ 输出结果: Groovy Grails JAV ...
- winform 的 checklistbox动态绑定并选中值
绑定的代码:这里绑定的是一个泛型 BLL.PowerBLL powerbll = new BLL.PowerBLL(); checkpower.DataSource = powerbll.GetAll ...
- lucene学习-创建索引
本文的lucene是基于lucene3.5版本. 使用lucene实现搜索引擎开发,核心的部分是建立索引和搜索.本节主要是记录创建索引部分的内容. 创建的索引结构如图所示. 创建索引的步骤分为以下几个 ...
- point-position2修改版
说明: 在共面直线测试中,由于计算误差等原因,共面条件判断不准,但计算结果依然正确. // point-position2.cpp : 定义控制台应用程序的入口点. #include "st ...
- zoj2770 Burn the Linked Camp --- 差分约束
有n个营地,每一个营地至多容纳Ci人.给出m个条件:第i到第j个营地之间至少有k人. 问n个营地总共至少有多少人. 此题显然差分约束.要求最小值.则建立x-y>=z方程组,建图求最长路. 用d[ ...
- PHP实现对短信验证码发送次数的限制(防机刷验证码)
PHP实现对短信验证码发送限制(防止机刷验证码) 对用户获取短信验证码的手机号.ip.和浏览器(使用唯一标识)进行限制.本文介绍的方法是对用户每天只能通过同一浏览器或同一ip地址获取验证码10次或者同 ...
- VS2008试用版到期解决办法----win7下VS2008升级补丁.zip
打开好久没用的Visual studio 2008,才知道试用版已经到期了.在网上找来了序列号,可是连一个输入序列号的地方都没有,困惑了好久,终于找到了解决办法. 首先献上自己收集的Visual st ...
- VMware下安装centos6.7的步骤
一.虚拟机的创建 1.点击创建新的虚拟机(图片红框的位置) 2.选择自定义安装.下一步(根据自己的需要有选择的进行选择) 3.新装的虚拟机的版本和虚拟机兼容的产品以及这个版本的虚拟机的限制(这个窗口没 ...
- cocos2d-x中关于touch事件的响应
原作者:有缘人 来源:新浪微博 地址:http://blog.sina.com.cn/s/blog_6ac2c7260102vvdu.html 一.touch事件响应分为单点触摸响应和多点触摸响应. ...
- Cocos2d-x Lua中多场景切换生命周期
在多个场景切换时候,场景的生命周期会更加复杂.这一节我们介绍一下场景切换生命周期.多个场景切换时候分为几种情况:情况1,使用pushScene函数从实现GameScene场景进入SettingScen ...