FB面经 Prepare: Even Tree
You are given a tree (a simple connected graph with no cycles). The tree has nodes numbered from to and is rooted at node .
Find the maximum number of edges you can remove from the tree to get a forest such that each connected component of the forest contains an even number of vertices.
o
/ | | \
o o o o
|
o
比如可以删掉一个边变成:
o
x | | \
o o o o
|
o
结果里有两个tree,分别有2个和四个node,符合条件,这就是答案,因为再删就不符合条件了
return是一个list,里面是所有新生成的tree的root
我觉得题中应该再加上一个条件,就是guarantee是能够分割的,不然没法做. 如果总node总数是奇数的话, 怎么删都没法保证所有的子树是even number,所以这题的前提是node总数为偶数?
网上看到别人的很好的解法:
特别是用iterator.next()以后用iterator.remove()
public class TreeNode{
int val;
List<TreeNode> subtree;
public TreeNode(int val){
this.val = val;.
subtree = new ArrayList<>();
}
public void addChild(TreeNode child){
subtree.add(child);
}
}
public class BreakTree {
public List<TreeNode> breakTree(TreeNode root){
List<TreeNode> result = new ArrayList<>();
countAndBreak(result, root);
return result;
}
private int countAndBreak(List<TreeNode> result, TreeNode root){
if (root == null){
return 0;
}
Iterator<TreeNode> iter = root.subtree.iterator();
while (iter.hasNext()){
int childCount = countAndBreak(result, iter.next());
if (childCount == 0){
iter.remove();
} else{
count += childCount;
}
}
if (count % 2 == 0){
result.add(root);
return 0;
} else{
return count;
}
}
public static void main(String[] args){
TreeNode root = new TreeNode(0);
TreeNode firstChild = new TreeNode(1);
firstChild.addChild(new TreeNode(2));
root.addChild(firstChild);
root.addChild(new TreeNode(3));
root.addChild(new TreeNode(4));
root.addChild(new TreeNode(5));
BreakTree soln = new BreakTree();
List<TreeNode> result = soln.breakTree(root);
System.out.println(result.size());
}
}
FB面经 Prepare: Even Tree的更多相关文章
- FB面经 Prepare: LCA of Deepest Nodes in Binary Tree
给一个 二叉树 , 求最深节点的最小公共父节点 . retrun . 先用 recursive , 很快写出来了, 要求用 iterative . 时间不够了... Recursion: 返回的时候返 ...
- FB面经 Prepare: All Palindromic Substrings
Given a string, calculate how many substring is palindrome. Ignore non-char characters. Ignore case; ...
- FB面经 Prepare: Task Schedule
tasks has cooldown time, give an input task id array, output finish time input: AABCA A--ABCA output ...
- FB面经 Prepare: Make Parentheses valid
给一组括号,remove最少的括号使得它valid 从左从右各scan一次 package fb; public class removeParen { public static String fi ...
- FB面经Prepare: Friends Recommendation
有个getFriend() API, 让你推荐你的朋友的朋友做你的朋友,当然这个新朋友不能是你原来的老朋友 package fb; import java.util.*; public class R ...
- FB面经Prepare: Dot Product
Conduct Dot Product of two large Vectors 1. two pointers 2. hashmap 3. 如果没有额外空间,如果一个很大,一个很小,适合scan小的 ...
- FB面经prepare: Count the number of Vector
给一个超级大的排好序的vector [abbcccdddeeee]比如,要求返回[{,a}, {,b}, {,c}, {,d}, {,e}......]复杂度要优于O(N) 分析: 如果是binary ...
- FB面经 Prepare: Largest Island
Find largest island in a board package fb; public class LargestIsland { public int findLargestIsland ...
- FB面经prepare: task schedule II
followup是tasks是无序的. 一开始是有序的,比如说1, 1, 2, 1,一定要先执行第一个task1,然后等task1恢复,再执行第2个task1,再执行task2..... follow ...
随机推荐
- 解决ubuntu下firefox无法在线播放音频和视频的问题
原因 Ubuntu 为了规避专利和版权问题,很多东西没有预装,比如音视频解码器AAC. 那么为什么明明 Ubuntu 上没有AAC解码器, Chrome 却可以正常播放呢,自然的想法是 Chrome ...
- [jzoj]1115.【HNOI2008】GT考试
Link https://jzoj.net/senior/#main/show/1115 Description 申准备报名参加GT考试,准考证号为n位数X1X2X3...Xn-1Xn(0<=X ...
- Angularjs判断页面是否已经渲染结束(动态给标签长度)
相信大家都会碰到这样的问题.页面循环li.但是因为个数不知道.没有办法给li设置固定宽度.那么这时就需要动态计算数据长度并动态改变li的宽度 <!--周边信息--> <div cla ...
- 在Windows上安装Nexus 3.2.0-01
在Windows上安装Nexus 环境: Windows 7 apache-maven-3.3.9 JDK 1.8 下载Nexus: https://sonatype-download.globa ...
- GMA Round 1 新程序
传送门 新程序 程序框图如图所示,当输入的n=时,输出结果的ans是多少? 容易看出该程序求n以内质数个数,50以内有15个. 定位:简单题
- HTML5_新标签
HTML5 是定义 HTML 标准的最新版本. 是一个新版本的 HTML 语言,具有新的元素,属性,行为, 是一个技术及,允许更多样化和强大的网站和应用程序 优势: 跨平台: 通吃 MAC PC Li ...
- mysql 插入百万条数据
利用mysql内存表插入速度快的特点,先存储过程在内存表中生成数据,然后再从内存表插入普通表中 一.创建内存表 CREATE TABLE `vote_record_memory` ( `id` ) N ...
- 纯js自动批量引入js、css插件,支持自定义参数
//autoload.js ;! function(e) { var autoload = e.autoload || {}; e.autoload = autoload; e.autoload = ...
- Android adb 串口调试
adb (串口输入) echo 1 > /sys/class/remount/need_remount; mount -o rw,remount /system ...
- Java将Excel解析为数组集合
Java将Excel解析为数组集合 相关 jar 包: jxl-2.6.jar jar 包下载:http://files.cnblogs.com/files/liaolongjun/excel-jar ...