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 ...
随机推荐
- kvm虚拟化1
计算机的五大组成部分: 运算器,控制器,存储器,输入,输出 虚拟化是对cpu ,内存,,磁盘, 网络,IO 的虚拟 cpu的虚拟 以时间分片形式进行,这样使得cpu可以运行多个进程 内存进行了空间 ...
- 如何让多个li居中于ul中间
设置ul的display:table,text-align:center. 注意:不可以设置ul的宽,不然无法实现.
- 洛谷.5283.[十二省联考2019]异或粽子(可持久化Trie 堆)
LOJ 洛谷 考场上都拍上了,8:50才发现我读错了题=-= 两天都读错题...醉惹... \(Solution1\) 先求一遍前缀异或和. 假设左端点是\(i\),那么我们要在\([i,n]\)中找 ...
- django——面试题(已工作,暂停更新)
谈谈你对HTTP协议的认识. 什么是协议? 协议,是指通信的双方,在通信流程或内容格式上,共同遵守的标准. 什么是http协议? http协议,是互联网中最常见的网络通信标准. http协议的特点 ① ...
- XVIII Open Cup named after E.V. Pankratiev. Grand Prix of Khamovniki
A. Ability Draft 记忆化搜索. #include<stdio.h> #include<iostream> #include<string.h> #i ...
- LOJ 6270
最近(一直)有点(很)蠢 按照区间大小排序做区间包含多少区间的话 只用考虑 左端点比当前左端点小的和右端点比当前右端点大的,因为不可能同时满足 关于K,就在做到K的时候减一下就好了,一直傻逼在这了 # ...
- 如何使用JavaScript实现纯前端读取和导出excel文件
js-xlsx 介绍 由SheetJS出品的js-xlsx是一款非常方便的只需要纯JS即可读取和导出excel的工具库,功能强大,支持格式众多,支持xls.xlsx.ods(一种OpenOffice专 ...
- Vue(二十五)打包后路径报错问题
1.修改 config - index.js 2.修改 build - utils.js
- HTML5_图片合成_刮刮卡
刮刮卡(图片合成) 定义: globalCompositeOperation 属性,设置或返回如何将源图像 将 myCanvas 的背景图设置为一张图片,(刮开后显示) // 目标图像(已有的,外面一 ...
- [LeetCode] Generate Random Point in a Circle 生成圆中的随机点
Given the radius and x-y positions of the center of a circle, write a function randPoint which gener ...