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.

思想:如果如果p,q 比root小, 则LCA必定在左子树, 如果p,q比root大, 则LCA必定在右子树. 如果一大一小, 则root即为LCA.

代码如下:

/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {
public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
if(root==null) return root;
if(root.val>=p.val && root.val<=q.val || root.val<=p.val && root.val>=q.val)
return root;
else if(root.val>=p.val && root.val>=q.val)
return lowestCommonAncestor(root.left,p,q);
else
return lowestCommonAncestor(root.right,p,q); }
}

  运行结果:

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

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

  7. Java for LeetCode 235 Lowest Common Ancestor of a Binary Search Tree

    递归实现如下: public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) { if(p.val>ro ...

  8. Leetcode 235 Lowest Common Ancestor of a Binary Search Tree 二叉树

    给定一个二叉搜索树的两个节点,找出他们的最近公共祖先,如, _______6______ / \ ___2__ ___8__ / \ / \ 0 4 7 9 / \ 3 5 2和8的最近公共祖先是6, ...

  9. 【easy】235. Lowest Common Ancestor of a Binary Search Tree

    题意大概是,找两个节点的最低公共祖先 /** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNod ...

随机推荐

  1. Log4j 使用总结

    在实际编程时,要使Log4j真正在系统中运行事先还要对配置文件进行定义.定义步骤就是对Logger.Appender及Layout的分别使用.Log4j支持两种配置文件格式,一种是XML格式的文件,一 ...

  2. mysql性能的检查和调优方法

    mysql性能的检查和调优方法 发布时间:2009 年 10 月 4 日 发布者: OurMySQL 来源:sudone.com   才被阅读:3,524 次    才1条评论    我一直是使用my ...

  3. es6语法重构react代码

    1.使用React.Component创建组件,需要通过在constructor中调用super()将props传递给React.Component.另外react 0.13之后props必须是不可变 ...

  4. sklearn基础知识-准备阶段

    6.标签特征二元化 处理分类变量还有另一种方法,不需要通过OneHotEncoder,我们可以用LabelBinarizer. 这是一个阈值与分类变量组合的方法. In [1]: from sklea ...

  5. SharePoint 2010 最佳实践学习总结------第2章 SharePoint Windows PowerShell指南

    第2章 SharePoint Windows PowerShell指南 SharePoint 2010是SharePoint系列产品中第一个开始支持Windows PowerShell的产品,在以前的 ...

  6. 区别: @Secured(), @PreAuthorize() 及 @RolesAllowed()

    在Spring security的使用中,为了对方法进行权限控制,通常采用的三个注解,就是@Secured(), @PreAuthorize() 及 @RolesAllowed(). 但是着三者之间的 ...

  7. oracle数据库数据导出和导入

    oracle的客户端里面的bin下面有两个可执行文件,名称分别为exp.exe和imp.exe. 他俩的用途就是导出和导入数据用的. 全库 导出:exp 用户名/密码@数据库名 full=y file ...

  8. SqlDataAdapter怎么处理事务呢

    SqlDataAdapter怎么处理事务呢,没找到相关代码,哪位朋友能提供下   解决方案 » this._DBAdpt-------------SqlDataAdapterthis._DBConn- ...

  9. SPOJ #536. How many Fibs

    Since number could be 10^100, we have to use large number processing method, in string. A simpler me ...

  10. android学习笔记22——Notification

    Notification ==> Notification是显示在手机状态栏的消息,位于手机屏幕的最上方: 一般显示手机当前网络.电池状态.时间等: Notification所代表的是一种全局效 ...