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 ...
随机推荐
- 毕向东—Java基础知识总结(超级经典)
Java基础知识总结(超级经典) 写代码: 1,明确需求.我要做什么? 2,分析思路.我要怎么做?1,2,3. 3,确定步骤.每一个思路部分用到哪些语句,方法,和对象. 4,代码实现.用具体的java ...
- WEB测试重点
1.功能测试:所实现的功能是否和需求一致:2.界面测试:界面是否美观,风格是否一致,文字内容是否正确:3.链接测试:打开链接速度是否合理:是否链接到正确的页面:是否有空白页面:4.性能测试:系统能支持 ...
- POJ 1208 The Blocks Problem --vector
http://poj.org/problem?id=1208 晚点仔细看 https://blog.csdn.net/yxz8102/article/details/53098575 #include ...
- P1516 青蛙的约会
P1516 青蛙的约会x+mt-p1L=y+nt-p2L(m-n)t+L(p2-p1)=y-x令p=p2-p1(m-n)t+Lp=y-x然后套扩欧就完事了 #include<iostream&g ...
- CSS---通向臃肿的道路(关于 “separation of concerns” (SoC)的原则)
When it comes to CSS, I believe that the sacred principle of “separation of concerns” (SoC) has lead ...
- windows安装nexus3
1.下载nexus3 https://www.sonatype.com/download-oss-sonatype 2.解压文件D:\javatool\ 3.在path中配置环境变量 D:\javat ...
- fflush()函数:更新缓冲区
fflush()的作用是用来刷新缓冲区: fflush(stdin)刷新标准输入缓冲区,把输入缓冲区里的东西丢弃:stdin是standard input的缩写,即标准输入,一般是指键盘:标准输入缓冲 ...
- Android的Launcher启动流程 “Launcher部分启动流程”
研究代码从:AndroidManifest.xml.自定义的Application.java开始. Android系统启动时,系统需要一个Home应用程序来负责将这些应用程序展示出来:也就是该应用的目 ...
- js两个箭头 =>()=>()
request(_ action)let withStatus =status =>action=> R.merge(action, (status])let request = with ...
- SD卡
一.SD卡接口 SD 卡的接口可以支持两种操作模式:主机系统可以选择以上其中任一模式, SD 卡模式允许 4 线的高速数据传输. SPI 模式允许简单通用的 SPI 通道接口, 这种模式相对于 SD ...