FB面经 Prepare: LCA of Deepest Nodes in Binary Tree
给一个 二叉树 , 求最深节点的最小公共父节点
return .
retrun .
先用 recursive , 很快写出来了, 要求用 iterative 。 时间不够了。。。
Recursion: 返回的时候返回lca和depth,每个node如果有大于一个子节点的depth相同就返回这个node,如果有一个子节点depth更深就返回个子节点lca,这个o(n)就可以了
Iteration: tree的recursion换成iteration处理,一般用stack都能解决吧(相当于手动用stack模拟recursion)。感觉这题可以是一个样的做法,换成post order访问,这样处理每个node的时候,左右孩子的信息都有了,而且最后一个处理的node一定是tree root
我的想法是要用hashMap<TreeNode, Info>
class Info{
int height;
TreeNode LCA;
}
1 package fbOnsite;
public class LCA2 {
private class ReturnVal {
public int depth; //The depth of the deepest leaves on the current subtree
public TreeNode lca;//The lca of the deepest leaves on the current subtree
public ReturnVal(int d, TreeNode n) {
depth = d;
lca = n;
}
}
public TreeNode LowestCommonAncestorOfDeepestLeaves(TreeNode root) {
ReturnVal res = find(root);
return res.lca;
}
private ReturnVal find(TreeNode root) {
if(root == null) {
return new ReturnVal(0, null);
} else {
ReturnVal lRes = find(root.left);
ReturnVal rRes = find(root.right);
if(lRes.depth == rRes.depth) {
return new ReturnVal(lRes.depth+1, root);
} else {
return new ReturnVal(Math.max(rRes.depth, lRes.depth)+1, rRes.depth>lRes.depth?rRes.lca:lRes.lca);
}
}
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
TreeNode t1 = new TreeNode(1);
TreeNode t2 = new TreeNode(2);
TreeNode t3 = new TreeNode(3);
TreeNode t4 = new TreeNode(4);
TreeNode t5 = new TreeNode(5);
TreeNode t6 = new TreeNode(6);
TreeNode t7 = new TreeNode(7);
t1.left = t2;
t1.right = t3;
t2.left = t4;
//t3.left = t5;
//t3.right = t6;
t2.right = t7;
LCA sol = new LCA();
TreeNode res = sol.LowestCommonAncestorOfDeepestLeaves(t1);
System.out.println(res.val);
}
}
FB面经 Prepare: LCA of Deepest Nodes in Binary Tree的更多相关文章
- Binary Tree: Write a function to return count of nodes in binary tree which has only one child.
June 8, 2015 我最喜欢的一道算法题目, 二行代码. 编程序需要很强的逻辑思维, 严密,我还没有很好训练自己.想一想, 二行代码, 五分钟就可以搞定; 最近这几天网上大家热议的 Homebr ...
- [LeetCode] Smallest Subtree with all the Deepest Nodes 包含最深结点的最小子树
Given a binary tree rooted at root, the depth of each node is the shortest distance to the root. A n ...
- [Swift]LeetCode865. 具有所有最深结点的最小子树 | Smallest Subtree with all the Deepest Nodes
Given a binary tree rooted at root, the depth of each node is the shortest distance to the root. A n ...
- 865. Smallest Subtree with all the Deepest Nodes 有最深节点的最小子树
[抄题]: Given a binary tree rooted at root, the depth of each node is the shortest distance to the roo ...
- LeetCode 865. Smallest Subtree with all the Deepest Nodes
原题链接在这里:https://leetcode.com/problems/smallest-subtree-with-all-the-deepest-nodes/ 题目: Given a binar ...
- 【LeetCode】865. Smallest Subtree with all the Deepest Nodes 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- U面经Prepare: Print Binary Tree With No Two Nodes Share The Same Column
Give a binary tree, elegantly print it so that no two tree nodes share the same column. Requirement: ...
- PAT A1151 LCA in a Binary Tree (30 分)——二叉树,最小公共祖先(lca)
The lowest common ancestor (LCA) of two nodes U and V in a tree is the deepest node that has both U ...
- 【PAT 甲级】1151 LCA in a Binary Tree (30 分)
题目描述 The lowest common ancestor (LCA) of two nodes U and V in a tree is the deepest node that has bo ...
随机推荐
- Scala模式匹配| 隐式转换
1. 模式匹配 Scala中的模式匹配类似于Java中的switch语法,但是更加强大.模式匹配语法中,采用match关键字声明,每个分支采用case关键字进行声明,当需要匹配时,会从第一个case分 ...
- 20172328 2018-2019《Java软件结构与数据结构》第三周学习总结
20172328 2018-2019<Java软件结构与数据结构>第三周学习总结 概述 Generalization 本周学习了第五章:队列.主要内容包含队列的处理过程.如何用对例如求解问 ...
- struts2的java.lang.NoSuchMethodException错误
不久前在学习struts时出现这个错误,在网上搜索了半天,发现答案不一.将其总结如下,以方便大家参考. 1. 你有没有试试看 其它的方法能不能用,要是都是这种情况的话,可能是你的Action类没有继承 ...
- JS this用法详解
随着对js的深入学习和使用,你会发现它里面包含了很多令人困惑的机制,比如对象.闭包.原型链继承等等 1.this是啥? 简言之,this是JavaScript语言中定义的众多关键字之一,它的特 ...
- CF444E. DZY Loves Planting
题目链接 CF444E. DZY Loves Planting 题解 可以..二分网络流 可是 考虑边从小到大排序 考虑每条边能否成为答案 用并查集维护节点之间的联通性 对于一条边来说,如果这条边可以 ...
- Hadoop日志文件
初学者运行MapReduce作业时,经常会遇到各种错误,往往不知所云,一般直接将终端打印的错误贴到搜索引擎上查找,以借鉴前人的经验. 对于hadoop而言,当遇到错误时,第一时间应是查看日志,日志里通 ...
- Java_并发工具包 java.util.concurrent 用户指南(转)
译序 本指南根据 Jakob Jenkov 最新博客翻译,请随时关注博客更新:http://tutorials.jenkov.com/java-util-concurrent/index.html.本 ...
- ArcGIS AddIn 批量设置栅格图层背景色为透明
protected override void OnClick() { // // TODO: Sample code showing how to access button host // Arc ...
- 移动端 上传头像 并裁剪功能(h5)
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta name ...
- 表单/iframe与video标签
<form action="所有表单值提交的地址" method="传值的方式默认是GET方式,还有另一种POST方式"> 表单元素</for ...