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. James Munkres Topology: Theorem 19.6

    Theorem 19.6 Let \(f: A \rightarrow \prod_{\alpha \in J} X_{\alpha}\) be given by the equation \[ f( ...

  2. 【JAVA】servlet 定时启动

    步骤一: web.xml中加上如下的代码: <load-on-startup>10</load-on-startup>这句话是重点. <servlet> <s ...

  3. Java 实现视频下载功能

    public static boolean httpDownload(String httpUrl, String saveFile) { // 1.下载网络文件 int byteRead; URL ...

  4. Aspnet Mvc 前后端分离项目手记(一) 关于跨域问题(还有前言)

    前言,最近的项目使用前后端分离的模式,记录其中一些知识点.经过这个项目,也对前后端分离有了更多理解,尤其是在技术之外的方面. 越来越多的项目采用前后端分离的原因,有两点:      1,技术方面的原因 ...

  5. 还不知道spring的RestTemplate的妙用吗

    为什么要使用RestTemplate? 随着微服务的广泛使用,在实际的开发中,客户端代码中调用RESTful接口也越来越常见.在系统的遗留代码中,你可能会看见有一些代码是使用HttpURLConnec ...

  6. zepto中的scrollTo

    //zepto没有scrollTo动画,这里扩展了一个scrollTo函数 $.fn.scrollTo = function(options) { var defaults = { scrollTop ...

  7. 关于FastReport在winform中的使用(包含FastReport.net的安装步骤链接)

    一.FastReport的简介 FastReport是功能齐全的报表控件,使开发者可以快速并高效地为·NET/VCL/COM/ActiveX应用程序添加报表支持. 二.FastReport的安装(推荐 ...

  8. ipdb介绍及Tensor

    ipdb介绍 1.现在IPython之外使用debug功能,则需要安装ipdb(pip install ipdb),而后在需要进入调试的地方加上如下代码即可: import ipdb ipdb.set ...

  9. [LeetCode] Random Pick with Blacklist 带黑名单的随机选取

    Given a blacklist B containing unique integers from [0, N), write a function to return a uniform ran ...

  10. 6、echarts使用的坑

    实例化id值要用唯一的,一个项目中不能用同一个id, 不然后面的样式覆盖前面的,且后面样式不显示.