FB面经Prepare: Find Longest Path in a Multi-Tree
给的多叉树, 找这颗树里面最长的路径长度
解法就是在子树里面找最大的两个(或一个,如果只有一个子树的话)高度加起来。
对于每一个treenode, 维护它的最高的高度和第二高的高度,经过该点的最大路径就是: 最高高度+第二高高度,然后return 最高高度
package fbPractise;
import java.util.*;
class TreeNode {
int val;
List<TreeNode> children;
public TreeNode(int value) {
this.val = value;
this.children = new ArrayList<TreeNode>();
}
}
public class LongestPathInTree {
static int maxLen = 0;
public static int findLongestPath(TreeNode node) {
findMaxPath(node);
return maxLen;
}
public static int findMaxPath(TreeNode node) {
if (node == null) return 0;
int heightest1 = 0;
int heightest2 = 0;
for (TreeNode child : node.children) {
int childHeight = findMaxPath(child);
if (childHeight > heightest1) {
heightest2 = heightest1;
heightest1 = childHeight;
}
else if (childHeight > heightest2) {
heightest2 = childHeight;
}
}
maxLen = Math.max(maxLen, 1 + heightest1 + heightest2);
return 1 + heightest1;
}
public static void main(String[] args) {
TreeNode node1 = new TreeNode(1);
TreeNode node2 = new TreeNode(2);
TreeNode node3 = new TreeNode(3);
TreeNode node4 = new TreeNode(4);
TreeNode node5 = new TreeNode(5);
TreeNode node6 = new TreeNode(6);
node1.children.add(node2);
node1.children.add(node3);
node2.children.add(node4);
node2.children.add(node5);
node5.children.add(node6);
int res = findLongestPath(node1);
System.out.println(res);
}
}
FB面经Prepare: Find Longest Path in a Multi-Tree的更多相关文章
- FB面经 Prepare: LCA of Deepest Nodes in Binary Tree
给一个 二叉树 , 求最深节点的最小公共父节点 . retrun . 先用 recursive , 很快写出来了, 要求用 iterative . 时间不够了... Recursion: 返回的时候返 ...
- Solve Longest Path Problem in linear time
We know that the longest path problem for general case belongs to the NP-hard category, so there is ...
- Why longest path problem doesn't have optimal substructure?
We all know that the shortest path problem has optimal substructure. The reasoning is like below: Su ...
- Summary: Lowest Common Ancestor in a Binary Tree & Shortest Path In a Binary Tree
转自:Pavel's Blog Now let's say we want to find the LCA for nodes 4 and 9, we will need to traverse th ...
- the longest distance of a binary tree
版权声明:欢迎查看本博客.希望对你有有所帮助 https://blog.csdn.net/cqs_2012/article/details/24880735 the longest distance ...
- 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: Count Unique Island
数unique island, 比如 110000 110001 001101 101100 100000 总共两个unique岛,不是四个 方法可以是记录每次新的岛屿搜索的路径,left,right ...
- FB面经 Prepare: Make Parentheses valid
给一组括号,remove最少的括号使得它valid 从左从右各scan一次 package fb; public class removeParen { public static String fi ...
随机推荐
- 编码 ASCII, GBK, Unicode+utf-8
0. 1.参考 网页编码就是那点事 阮一峰 字符编码笔记:ASCII,Unicode 和 UTF-8 2.总结 美国 ASCII 码 发音: /ˈæski/ :128个字符,只占用了一个字节的后面7位 ...
- Consideration about improving mathematics study
In this article, I’ll present my ideas about how to improve mathematics study, which are the forewor ...
- Redis 数据结构之dict(2)
本文及后续文章,Redis版本均是v3.2.8 上篇文章<Redis 数据结构之dict>,我们对dict的结构有了大致的印象.此篇文章对dict是如何维护数据结构的做个详细的理解. 老规 ...
- 全文搜索引擎——Solr
1.部署solr a.下载并解压Solr b.导入项目(独立项目): 将解压后的 server\solr-webapp 下的 webapp文件夹 拷贝到tomcat的webapps下,并重命名为 so ...
- 关于WQS二分算法以及其一个细节证明
应用分析 它的作用就是题目给了一个选物品的限制条件,要求刚好选$m$个,让你最大化(最小化)权值, 然后其特点就是当选的物品越多的时候权值越大(越小). 算法分析 我们先不考虑物品限制条件, 假定我们 ...
- bzoj 4842 [Neerc2016]Delight for a Cat 最小费用最大流,线性规划
题意:有n个小时,对于第i个小时,睡觉的愉悦值为si,打隔膜的愉悦值为ei,同时对于任意一段连续的k小时,必须至少有t1时间在睡觉,t2时间在打隔膜.如果要获得的愉悦值尽 量大,求最大的愉悦值和睡觉还 ...
- php curl 跨域情趣
function curl_post($url='',$postdata='',$options=array()){ $ch=curl_init($url); curl_setopt($ch,CURL ...
- python全栈开发 * background 定位 z-index * 180813
I back-ground 一.颜色的表示: 1.单词 2.rgb表示法 rgb:红色 绿色 蓝色 三原色 光学显示器每个像素都是由三原色的发光原件组成的,靠明亮度不同调成不同的颜色的. 用逗号隔开, ...
- 【LookLook文档】通过less 定制自己的Bootstrap 样式
闲话 学习还是看文档最快,看文档要仔细点,不可走马观花.感谢 “Bootstrap中文网” 思路 1.早在2015开始接触,由于公司中使用的是easyui 固一直没完全使用Bootstrap,开始接触 ...
- Python全栈-magedu-2018-笔记1
第一章 - Python 环境搭建 操作系统准备 准备Linux最小系统即可. 如果在虚拟机中克隆,MAC地址会变. 这里使用CentOS 6.5+ Pyenv 安装 安装git yum instal ...