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的更多相关文章

  1. FB面经 Prepare: LCA of Deepest Nodes in Binary Tree

    给一个 二叉树 , 求最深节点的最小公共父节点 . retrun . 先用 recursive , 很快写出来了, 要求用 iterative . 时间不够了... Recursion: 返回的时候返 ...

  2. FB面经 Prepare: All Palindromic Substrings

    Given a string, calculate how many substring is palindrome. Ignore non-char characters. Ignore case; ...

  3. FB面经 Prepare: Task Schedule

    tasks has cooldown time, give an input task id array, output finish time input: AABCA A--ABCA output ...

  4. FB面经 Prepare: Make Parentheses valid

    给一组括号,remove最少的括号使得它valid 从左从右各scan一次 package fb; public class removeParen { public static String fi ...

  5. FB面经Prepare: Friends Recommendation

    有个getFriend() API, 让你推荐你的朋友的朋友做你的朋友,当然这个新朋友不能是你原来的老朋友 package fb; import java.util.*; public class R ...

  6. FB面经Prepare: Dot Product

    Conduct Dot Product of two large Vectors 1. two pointers 2. hashmap 3. 如果没有额外空间,如果一个很大,一个很小,适合scan小的 ...

  7. FB面经prepare: Count the number of Vector

    给一个超级大的排好序的vector [abbcccdddeeee]比如,要求返回[{,a}, {,b}, {,c}, {,d}, {,e}......]复杂度要优于O(N) 分析: 如果是binary ...

  8. FB面经 Prepare: Largest Island

    Find largest island in a board package fb; public class LargestIsland { public int findLargestIsland ...

  9. FB面经prepare: task schedule II

    followup是tasks是无序的. 一开始是有序的,比如说1, 1, 2, 1,一定要先执行第一个task1,然后等task1恢复,再执行第2个task1,再执行task2..... follow ...

随机推荐

  1. 业务线--node中间层做一个透传的项目

    1,node中间层总结 1,ejs引入vue的js,路由层(直接透传,自定义行的),比较浅层的一层 中间件的引入 ? ) { // 与rd约定,接口成功返回code===0,其余为失败 console ...

  2. Problem J. Journey with Pigs

    Problem J. Journey with Pigshttp://codeforces.com/gym/241680/problem/J考察排序不等式算出来单位重量在每个村庄的收益,然后生序排列猪 ...

  3. Konva入门教程

    啥是 Konva Konva 是一个 canvas 库,可以让我们像操作 DOM 一样来操作 canvas,并提供了对 canvas 中元素的事件机制,拖放操作的支持.所以,用它来做一个拼图游戏什么的 ...

  4. react学习一篇就够了

    webstrom自动格式化代码 命令 js框架 MVC 安装 npm install create-react-app -g 生成项目(项目名npm发包包命名规范 /^[a-z0-9_-]$/) cr ...

  5. 解决ubuntu下firefox无法在线播放音频和视频的问题

    原因 Ubuntu 为了规避专利和版权问题,很多东西没有预装,比如音视频解码器AAC. 那么为什么明明 Ubuntu 上没有AAC解码器, Chrome 却可以正常播放呢,自然的想法是 Chrome ...

  6. 学习 Vim —— Vimtutor 总结笔记

    Lesson 2 2.1-2.3 删除 [dw] 删除从光标开始处至下一词开始前的部分,光标停在下一词的词首. [de] 删除从光标开始处至词尾的部分. [d$] 删除从光标开始处至行末的部分. 2. ...

  7. (一)主域相同子域不同之document.domain跨域

    一.什么是主域名,什么是子域名? 主域名又称一级域名或者顶级域名,由域名主体.域名后缀组成,比如cnblogs.com : 子域名有二级域名,比如www.cnblogs.com.三级域名,比如home ...

  8. IDEA激活方式(亲测有效)加汉化方式

    2018/12/3 最新破解方法 将0.0.0.0 account.jetbrains.com保存到本地host文件中 然后使用注册码 K71U8DBPNE-eyJsaWNlbnNlSWQiOiJLN ...

  9. Codeforces Round #547 (Div. 3)

    我老人家走了四公里吃个汉堡还没吃成.垃圾肯德基.垃圾春分半价桶. 蜜雪冰城百香果加冰+烤串真是爽死了.原来二十多块钱可以吃的这么爽. A: #include <bits/stdc++.h> ...

  10. 8、vue路由跳转 params与query 路由传参

    params与query router文件下index.js里面,是这么定义路由的: { path: '/about', name: 'About', component: About } 用quer ...