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

According to the definition of LCA on Wikipedia: “The lowest common ancestor is defined between two nodes v and w as the lowest node in T that has both v and w as descendants (where we allow a node to be a descendant of itself).”

        _______6______
/ \
___2__ ___8__
/ \ / \
0 _4 7 9
/ \
3 5

For example, the lowest common ancestor (LCA) of nodes 2 and 8 is 6. Another example is LCA of nodes 2 and 4 is 2, since a node can be a descendant of itself according to the LCA definition.

解题思路:

节点a与节点b的公共祖先c一定满足:a与b分别出现在c的左右子树上(如果a或者b本身不是祖先的话)。

Java代码:

class TreeNode {
int val;
TreeNode left;
TreeNode right;
TreeNode(int x) { val = x; }
} public class Solution {
public static TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
if(root==null) return null;
if(root==p||root==q) return root;
TreeNode L=lowestCommonAncestor(root.left,p,q);
TreeNode R=lowestCommonAncestor(root.right,p,q);
if(L!=null&&R!=null) return root;
return L!=null?L:R;
}
//test
public static void main(String[] args) {
// TODO Auto-generated method stub
TreeNode tr=new TreeNode(1);
TreeNode nodeB=new TreeNode(2);
TreeNode nodeC=new TreeNode(3);
TreeNode nodeD=new TreeNode(4);
TreeNode nodeE=new TreeNode(5);
tr.left=nodeB;
tr.right=nodeC;
tr.left.right=nodeD;
tr.left.left=nodeE;
System.out.print(lowestCommonAncestor(tr, nodeC, nodeE).val);
} }

LeetCode (236):Lowest Common Ancestor of a Binary Search Tree的更多相关文章

  1. leetcode 235. Lowest Common Ancestor of a Binary Search Tree 236. Lowest Common Ancestor of a Binary Tree

    https://www.cnblogs.com/grandyang/p/4641968.html http://www.cnblogs.com/grandyang/p/4640572.html 利用二 ...

  2. [LeetCode] 235. 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 ...

  3. 235.236. Lowest Common Ancestor of a Binary (Search) Tree -- 最近公共祖先

    235. Lowest Common Ancestor of a Binary Search Tree Given a binary search tree (BST), find the lowes ...

  4. [LeetCode] 235. 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 ...

  5. LeetCode 235. 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. leetcode 235. 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 ...

  7. (easy)LeetCode 235.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. 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 ...

  9. Java [Leetcode 235]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 ...

随机推荐

  1. YAML 语言

    1. YAML 介绍 YAML:以数据为中心,比JSON,XML更适合做配置文件; //示例: server: port: 8081 //此处有空格 2. YAML 基本语法 使用缩进表示层级关系; ...

  2. 627. Swap Salary

    627. Swap Salary SQL Schema Given a table salary, such as the one below, that has m=male and f=femal ...

  3. python包管理一防丢失

    pip3 freeze >list.txt      导出当前环境安装的所有包(list是当前项目录下的文件,可以自己命名)pip3 install -r list.txt     安装文件中所 ...

  4. EditText把回车键变成搜索

    版权声明:本文为博主原创文章.未经博主同意不得转载. https://blog.csdn.net/mingyue_1128/article/details/31376159 在xml文件里的EditT ...

  5. Spring MVC学习(五)---ModelAndView没有明显申明name

    看图不解释: 对于这种写法: new ModelAndView().addObject(XXX)  

  6. usb-tooltip 重写.tooltip { word-break: break-all; }解决单词内换行

    <div style="padding: 5px 10px; font-size: 16px; text-align: left" class="truncate& ...

  7. 基于python3.6.6的scrapy环境部署+图像识别插件安装

    一.Python3.6.6安装1.安装依赖的二进制软件包yum -y install zlib zlib-devel bzip2 bzip2-devel ncurses ncurses-devel r ...

  8. 我在Xcode 6上Swift框架的测试经验分享

    我耗费了两个多月时间来琢磨Swift作为一门函数是编程语言都能做些什么,而今已经转移 到使用Swift来开发库文件了. 我花了一天的时间,最后发觉之前做的Swift特性探究是相当愉快的经历,我发现仍旧 ...

  9. TraClus java版实现

           前一阵子我们部门接到了业务那边的一个需求.想通过用户的wifi数据计算出商场内用户最喜爱走的线路.其实说白了就是用户轨迹聚类.根据业务的需求,我们最终采用了traClus聚类算法.tra ...

  10. 斯坦福第二课:单变量线性回归(Linear Regression with One Variable)

    二.单变量线性回归(Linear Regression with One Variable) 2.1  模型表示 2.2  代价函数 2.3  代价函数的直观理解 I 2.4  代价函数的直观理解 I ...