给一个 二叉树 , 求最深节点的最小公共父节点

           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的更多相关文章

  1. Binary Tree: Write a function to return count of nodes in binary tree which has only one child.

    June 8, 2015 我最喜欢的一道算法题目, 二行代码. 编程序需要很强的逻辑思维, 严密,我还没有很好训练自己.想一想, 二行代码, 五分钟就可以搞定; 最近这几天网上大家热议的 Homebr ...

  2. [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 ...

  3. [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 ...

  4. 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 ...

  5. LeetCode 865. Smallest Subtree with all the Deepest Nodes

    原题链接在这里:https://leetcode.com/problems/smallest-subtree-with-all-the-deepest-nodes/ 题目: Given a binar ...

  6. 【LeetCode】865. Smallest Subtree with all the Deepest Nodes 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

  7. 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: ...

  8. 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 ...

  9. 【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 ...

随机推荐

  1. javascript 通过模块模式实现代码访问控制

    <script type="text/javascript"> // 在匿名函数中使用var关键字 (function(){ var privateField = 42 ...

  2. excel数据导入mysql

    先把excel数据另存成txt文件 Load Data InFile 'D:/1.txt' Into Table `res_type_collect` fields terminated by '@‘ ...

  3. 单元测试如何覆盖internal的方法

    在类的设计中经常会有类或者方法要设置成private或者internal等方式,在使用中这么做无可厚非,但是对单元测试的影响也颇大 对于private方法,那只有做一个副本然后改成internal或p ...

  4. 必须知道的Linux内核常识详解

    一.内核功能.内核发行版 1.到底什么是操作系统 (1)linux.windows.android.ucos就是操作系统: (2)操作系统本质上是一个程序,由很多个源文件构成,需要编译连接成操作系统程 ...

  5. 2018-2019-1 20189201 《LInux内核原理与分析》第七周作业

    我的愿望是 好好学习Linux 一.书本第六章知识总结[进程的描述和进程的创建] 基础知识1 操作系统内核实现操作系统的三大管理功能,即进程管理功能,内存管理和文件系统.对应的三个抽象的概念是进程,虚 ...

  6. 当linux报 “-bash: fork: 无法分配内存”

    “-bash: fork: 无法分配内存”,发现连了好多终端,然后断开了一个终端,然后这边终端可以敲命令了 [root@172.16.31.105 /home/www/test]# free -m  ...

  7. E - Andrew and Taxi-二分答案-topo判环

    E - Andrew and Taxi 思路 :min max   明显二分答案,二分需要破坏的那些边的中机器人数量最多的那个. check 过程建边时直接忽略掉小于 mid 的边,这样去检验有无环存 ...

  8. django——文本编辑器

    在博客项目中,为了支持用户的在线编辑博客,我们选用了kindeditor这个强大的编辑器. 以下是对kindeditor的简介,以及在Django中引入这个编辑器的方法:) 1.KindEditor是 ...

  9. Django路由机制

    Django的URL路由配置: Settings.py文件中的ROOT_URLCONF变量指定全局路由文件名称 ROOT_URLCONF = '工程名称.urls'   #默认对应工程目录下urls. ...

  10. IDEA激活方式(亲测有效)加汉化方式

    2018/12/3 最新破解方法 将0.0.0.0 account.jetbrains.com保存到本地host文件中 然后使用注册码 K71U8DBPNE-eyJsaWNlbnNlSWQiOiJLN ...