Given the root and two nodes in a Binary Tree. Find the lowest common ancestor(LCA) of the two nodes.

The lowest common ancestor is the node with largest depth which is the ancestor of both nodes.

Example

    /     \

          /     \

For  and , the LCA is .

For  and , the LCA is .

For  and , the LCA is .

更复杂的参考:http://www.cnblogs.com/EdwardLiu/p/4265448.html

 public class Solution {
/**
* @param root: The root of the binary search tree.
* @param A and B: two nodes in a Binary.
* @return: Return the least common ancestor(LCA) of the two nodes.
*/
public TreeNode lowestCommonAncestor(TreeNode root, TreeNode A, TreeNode B) {
// write your code here
if (root == null) return null;
if (root==A || root==B) return root;
TreeNode lch = lowestCommonAncestor(root.left, A, B);
TreeNode rch = lowestCommonAncestor(root.right, A, B);
if (lch!=null && rch!=null) return root;
return lch==null? rch : lch;
}
}

Lintcode: Lowest Common Ancestor的更多相关文章

  1. 88 Lowest Common Ancestor of a Binary Tree

    原题网址:https://www.lintcode.com/problem/lowest-common-ancestor-of-a-binary-tree/description 描述 给定一棵二叉树 ...

  2. 【Lintcode】088.Lowest Common Ancestor

    题目: Given the root and two nodes in a Binary Tree. Find the lowest common ancestor(LCA) of the two n ...

  3. [OJ] Lowest Common Ancestor

    LintCode 88. Lowest Common Ancestor (Medium) LeetCode 236. Lowest Common Ancestor of a Binary Tree ( ...

  4. [LeetCode] Lowest Common Ancestor of a Binary Tree 二叉树的最小共同父节点

    Given a binary tree, find the lowest common ancestor (LCA) of two given nodes in the tree. According ...

  5. [LeetCode] Lowest Common Ancestor of a Binary Search Tree 二叉搜索树的最小共同父节点

    Given a binary search tree (BST), find the lowest common ancestor (LCA) of two given nodes in the BS ...

  6. 48. 二叉树两结点的最低共同父结点(3种变种情况)[Get lowest common ancestor of binary tree]

    [题目] 输入二叉树中的两个结点,输出这两个结点在数中最低的共同父结点. 二叉树的结点定义如下:  C++ Code  123456   struct BinaryTreeNode {     int ...

  7. [LeetCode]Lowest Common Ancestor of a Binary Search Tree

    Given a binary search tree (BST), find the lowest common ancestor (LCA) of two given nodes in the BS ...

  8. 数据结构与算法(1)支线任务4——Lowest Common Ancestor of a Binary Tree

    题目如下:https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-tree/ Given a binary tree, fin ...

  9. Lowest Common Ancestor of a Binary Search Tree

    Given a binary search tree (BST), find the lowest common ancestor (LCA) of two given nodes in the BS ...

随机推荐

  1. 网狐荣耀平台找不到存储过程 'GSP_GS_LoadGameMatchItem'错误解决

    把RYGameMatchDB的存储过程复制到RYGameScoreDB即可,GSP_GS_InsertGameMatchItem和GSP_GS_DeleteGameMatchItem也一样 由于存储过 ...

  2. 2555: SubString[LCT+SAM]

    2555: SubString Time Limit: 30 Sec  Memory Limit: 512 MB Submit: 2601  Solved: 780 [Submit][Status][ ...

  3. magent实现memcached集群的一个问题

    之前我们小组封装了一个memcached类库,里面有一个名为RemoveStartWith的方法可以根据起始字符串删除所有节点中负责键值规则的缓存项.它实现的原理就是通过stats命令获取每个节点的所 ...

  4. iOS8跳转到系统设置页

    版权声明:本文为博主原创文章,未经博主允许不得转载. 大家都知道,在iOS5.0时时可以跳转到系统的设置页的.但是在5.1之后就不可以了. 刚才研究了下这个问题,发现只有iOS8可以跳转到系统设置里自 ...

  5. CentOS安装php及其扩展

    列出所有的可安装的软件包 yum list | grep php56w* | grep redis 安装php及其扩展 yum install  -y php56w php56w-mysql php5 ...

  6. iOS - 视频播放处理全屏/横屏时候遇见的坑

    视频播放想要全屏,使用shouldAutorotate方法禁止主界面,tabbar控制器横屏,导致push进入播放页面不能横屏的问题... - (BOOL)shouldAutorotate { ret ...

  7. HDU 1213 - How Many Tables - [并查集模板题]

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1213 Today is Ignatius' birthday. He invites a lot of ...

  8. Git添加Gitee远程仓库

    1.使用IDEA初始化仓库,并提交代码 2.使用 git remote add origin https://gitee.com/你的码云用户名/XXXX //添加远程仓库 3.使用 git pull ...

  9. (转)Spring实现IoC的多种方式

    原文地址:http://www.cnblogs.com/best/p/5727935.html 目录 一.使用XML配置的方式实现IOC 二.使用Spring注解配置IOC 三.自动装配 四.零配置实 ...

  10. 在Java中谈尾递归--尾递归和垃圾回收的比较

    我不是故意在JAVA中谈尾递归的,因为在JAVA中谈尾递归真的是要绕好几个弯,只是我确实只有JAVA学得比较好,虽然确实C是在学校学过还考了90+,真学得没自学的JAVA好 不过也是因为要绕几个弯,所 ...