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. jquery动态添加的元素不能直接应用事件方法的时候

    对于由 jQuery 动态生成的元素,如用 jQuery 给元素添加 class,或者直接添加一对 p 标签,不能直接绑定常用的事件,如 click.因为这些元素属于动态生成,除非采用 onclick ...

  2. 项目之初的模型设计与status状态字段

    0X01 开始做一个app的时候,要先做的是流程设计与数据库模型设计. 但做的模型设计往往是设置字段满足当前的需求,缺乏足够的经验,即使为以后的功能预留出位置,也无法考虑周全. 比如,刚开始做用户表, ...

  3. Linux中Buffer和Cache的区别

    1. Cache:缓存区,是高速缓存,是位于CPU和主内存之间的容量较小但速度很快的存储器,因为CPU的速度远远高于主内存的速度,CPU从内存中读取数据需等待很长的时间,而  Cache保存着CPU刚 ...

  4. python下的并发编程

    阅读目录 一 背景知识 二 python并发编程之多进程 三 python并发编程之多线程 四 python并发编程之协程 五 python并发编程之IO模型 六 补充:paramiko模块 七 作业 ...

  5. 手机号验证正则表达式+Demo(亲测完毕)

    以下为本人亲测过的验证手机号格式的demo,需要的小伙伴拿走不谢~<!DOCTYPE html><html><head><meta charset=" ...

  6. c#常用数值范围汇总

    short.MaxValue 32767 short.MinValue -32768 int.MaxValue 2147483647 int.MinValue -2147483648 long.Max ...

  7. Apache Flink系列(1)-概述

    一.设计思想及介绍 基本思想:“一切数据都是流,批是流的特例” 1.Micro Batching 模式 在Micro-Batching模式的架构实现上就有一个自然流数据流入系统进行攒批的过程,这在一定 ...

  8. vue数据变化的监控是如何做到的

    mvvm框架里的数据监控对象,包括 基本数据类型和对象, 对象分为对象和数组. 首先是对普通数据类型和对象的监控.其次是对数组的监控. 对对象的监控需要用到递归; <!DOCTYPE html& ...

  9. 黑盒测试实践——day04

    一.任务进展情况 通过昨天的选择和搜集资料,目前已成功安装好了testWriter,目前正在选择合适的web系统,进行测试. 二.存在的问题 安装TestWriter之前,需要安装SQLServre2 ...

  10. 滑动viewpage

    Adapter: package com.example.fashionyuan.Adatader; import android.support.v4.app.Fragment;import and ...