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. hadoop 日常问题汇总(持续更新)

    问题描述:每次执行hadoop的shell命令时均出现如下警告: [hadoop@MyDB01 ~]$ hadoop fs -ls / 16/09/25 07:59:13 WARN util.Nati ...

  2. C语言位操作--逻辑运算符组合

    假设读者熟悉普通代数与布尔代数,下面是部分常见的涉及到加法.减法与逻辑运算符的组合: a.        -x=~x+1 b.           =~(x-1) c.        ~x=-x-1 ...

  3. 安装pod

    1.ruby升级最新 sudo gem update -n /usr/local/bin --system 2. $ gem sources *** CURRENT SOURCES *** https ...

  4. 火狐浏览器adobe flash player

    最近Firefox火狐浏览器已停止Flash Player 18.0.0.203运行 在Mozilla官方公告中,该公司提到Flash Player 18.0.0.203插件存在已知安全漏洞,建议用户 ...

  5. 对Aspose.Cells Excel文件操作的扩展

    工作中对Excel操作的需求很是常见,今天其他项目组的同事在进行Excel数据导入时,使用Aspose.Cells Excel 遇到了些问题. 刚好闲来不忙,回想自己用过的Excel文件操作,有NPO ...

  6. Python面向对象之属性

    属性的定义和调用 1,定义时,在普通方法的基础上添加@property装饰器 2,定义时,属性仅有一个self参数 3,调用时,无需括号 vim day7-8.py #!/usr/bin/python ...

  7. HDU-1881 毕业bg (01背包变形)

    毕业bg Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submissi ...

  8. tun笔记

    https://www.kernel.org/doc/Documentation/networking/tuntap.txt 虚拟网卡 TUN/TAP 驱动程序设计原理 https://www.ibm ...

  9. 解决UITableView分割线距左边有距离的办法

    首先在viewDidLoad方法中加上如下代码: if ([self.tableView respondsToSelector:@selector(setSeparatorInset:)]) {    ...

  10. HTML标签_增加css样式

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...