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 ...
随机推荐
- python 的 virtualenv 环境搭建及 sublime 手动创建运行环境
一.安装 virtual env sudo pip install virtualenv二.进入一个空白的目录初始化 virtual env 的环境cd ~/workspace/python/virt ...
- 一道简单的dp题 --- Greenhouse Effect CodeForces - 269B
题目链接: https://vjudge.net/problem/36696/origin 题目大意: 要求从1到m升序排列,点可以随意移动,问最少需要移动多少次, 思路: 动态规划 可以推出转移方程 ...
- 探索JavaScript中Null和Undefined的深渊
当讨论JavaScript中的原始数据类型时,大多数人都知道的基本知识,从String,Number到Boolean.这些原始类型相当简单,行为符合常识.但是,本文将更多聚焦独特的原始数据类型Null ...
- 10树莓派Samba的安装与配置
2017-08-31 12:28:26 1.安装samba服务打开终端或者SSH连接树莓派,输入如下命令: sudo apt-get install samba 已经安装过了显示下列信息: pi@ra ...
- (50)Wangdao.com第七天_JavaScript 发展与简介
一个完整的JavaScript 应该由以下三部分组成: ECMAScript DOM,全称Browser Object Model,即浏览器对象模型,主要处理浏览器窗口和框架 BOM,全称Docume ...
- 匿名函数function前面的! ~等符号作用小解
好久没写博客了,刚过完年,给大家拜个晚年,大家新年快乐! 相信昨晚前端,很多同学应该都见过类似于: !function() {do something...}() ~function(){do som ...
- vue使用技巧(分页、nextTick、复制对象)
分页技巧 v1.0+ 版本的时候使用过滤器 limitBy 实现 v2.0+ 版本的时候使用compute使用 slice 方法实现 data:{ pageNum:10 }, computed:{ n ...
- 白盒测试实践-day02
一.任务进展情况 小组分工完成后,了解findbugs的使用过程,以及junit的测试步骤. 二.存在的问题 由于对单元测试不是太了解,导致无法进行测试. 三.解决方法 看mooc上面的视频,了解测试 ...
- Jedis自己整理比较全的API
package com.tebon.ams.utils; import com.alibaba.fastjson.JSON;import com.tebon.ams.util.ObjectUtil;i ...
- SQL - 2.基础语法
一.SQL分类: DDL—数据定义语言(CREATE,ALTER,DROP,DECLARE) DML—数据操纵语言(SELECT,DELETE,UPDATE,INSERT) DCL—数据控制语言(GR ...