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

           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. Redis数据结构之skiplist

    本文及后续文章,Redis版本均是v3.2.8 我们会经常选择使用sorted set数据结构,是由于其提供的操作非常丰富,可以满足非常多的应用场景.sorted set数据结构是由skiplist( ...

  2. Redis数据结构之intset(2)

    本文及后续文章,Redis版本均是v3.2.8 上文我们说到intset整型集合的数据结构定义即元素的添加和查询操作,本文我们来看下Redis暴露给外面使用的Set集合,先通过一些基本的命令回顾下se ...

  3. UOJ#110. 【APIO2015】Bali Sculptures 贪心 动态规划

    原文链接https://www.cnblogs.com/zhouzhendong/p/UOJ110.html 题解 我们发现n=2000 的子任务保证A=1! 分两种情况讨论: $n\leq 100$ ...

  4. 开源搜索引擎solr elasticsearch学习计划

    其实不单单是研究solr elasticsearch把,进行调研性技术学习时,应该制定一些目标以及里程碑.新的技术调研 学习是一件很爽的事,能学到新技术新东西.但是在学习新技术同时,有几个问题是需要我 ...

  5. day13_H5_CSS_1

    一.标签.HTML是可以被浏览器解析的一套规则 1.a标签:他是主动闭合标签,表现形式<a href="http://www.baidu.com">baidu<a ...

  6. defer 内追踪变量变化

    遇到一个需求,需要追踪变量的最终情况.defer比较合适,但是写了变量和指针都无效,于是试了试: 变量,变量地址,指针的使用情况 func TestDefer(t *testing.T) { a := ...

  7. centos7中安装pg数据库

    # centos中安装的命令 # yum install postgresql-server.x86_64 # 安装之前可以通过以下命令价差是否已经安装过 rpm -qa | grep postgre ...

  8. CSS3_过渡_2D 变换_瓶体旋转_动态时钟

    1. 过渡 transition 允许 CSS 的属性值在一定时间内平滑的过渡, 在鼠标点击,鼠标滑过或对属性改变中触发,并圆滑的改变 CSS 的属性值 简写属性: #box { width: 300 ...

  9. [LeetCode] Binary Tree Pruning 二叉树修剪

    We are given the head node root of a binary tree, where additionally every node's value is either a ...

  10. [Codeforces Round #508 (Div. 2)][Codeforces 1038E. Maximum Matching]

    前几天给舍友讲这题的时候感觉挺有意思的,就贴上来吧... 题目链接:1038E - Maximum Matching 题目大意:有\(n\)个棒子,每个条两端有颜色\(c1,c2\)以及他的价值\(v ...