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.


题目标签:Tree
  这道题目给了我们一个二叉搜索树,和两个点,让我们找到这两个点的最近的共同祖先。什么是最近的共同祖先的呢?来看看题目给的例子,如果给的两个点是3和5, 那么它们的LCA就是4;如果给的是3和9,那么它们的LCA就是6。这道题目我们要根据BST的特性来做,BST的每一个点,它的左边的孩子都比它小,右边的孩子都比它大。
  所以根据上述的这一点,我们每遍历一个点,就来判断一下要继续往左边走,还是往右边走,还是找到了祖先。
  这里有三种情况:(对于每一个遍历点)
    1. 拿p和q之间大的那一个 叫t-max, 和 遍历的点node比较一下,如果t-max 比node val 小的话,说明p和q 都在node 的左边,那么我们要继续走到左边取找;
    2. 拿p和q之间小的那一个,t-min, 和遍历的点node比较一下,如果t-min 比node val 大的话,说明p和q都在node 的右边,那么我们要继续走到右边取找;
    3. 剩下的情况就说明,p和q 在 node 的左右两边,或者是node 是p或者q, 另外一个点在node 的一边。 这种情况就是找到了祖先,把这一个node 返回。
 
 
 

Java Solution:

Runtime beats 62.62%

完成日期:07/04/2017

关键词:Tree (BST); LCA (lowest common ancestor)

关键点:只遍历左边或者右边根据BST的特性

 /**
* 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 null; // go into left child if the greater from p and q is smaller than root;
if(Math.max(p.val, q.val) < root.val)
return lowestCommonAncestor(root.left, p, q); // go into right child if the smaller from p and q is greater than root;
if(Math.min(p.val, q.val) > root.val)
return lowestCommonAncestor(root.right, p, q); // if p and q are not at the left side, and also not at the right side
// meaning p and q are at the both side or root is p or q , another one is at one side.
return root;
} }

参考资料:

http://www.cnblogs.com/grandyang/p/4640572.html

LeetCode 算法题目列表 - LeetCode Algorithms Questions List

LeetCode 235. Lowest Common Ancestor of a Binary Search Tree (二叉搜索树最近的共同祖先)的更多相关文章

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

  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 Lowest Common Ancestor of a Binary Search Tree 二叉搜索树的最近公共祖先

    给定一棵二叉搜索树, 找到该树中两个指定节点的最近公共祖先. 详见:https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-s ...

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

  5. 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 利用二 ...

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

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

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

随机推荐

  1. oracle客户端plsql设置字符集

    感谢一个新朋友的到来,我帮他的过程中有好些东西都不怎么想的起来了,所以从现在起我需要记录下每一点一滴, 因为我觉得写下来的东西才不会丢,而且欢迎以后的朋友到来. 使用plsql查数据的时候有时候中文会 ...

  2. jmeter通过BeanShell 脚本,实现对http请求参数的加密

    jmeter一直是一款很好的接口和性能测试工具,它是开源的,不需要为此支付任何费用,而且可以下载源码,可以在修改源代码并在此基础上拓展自己的功能或插件,它可以跟ant和jenkins结合起来搭建自己的 ...

  3. Openlayers系列(一)关于地图投影的理解

    背景 近期开发以MongoDB为基础的分布式地理数据管理平台系统,被要求做一个简单的demo给客户进行演示.于是笔者便打算向数据库中存储一部分瓦片数据,写一个简单的存取服务器,使用Openlayers ...

  4. spring实例化dataSource使用jndi和jdbc两种方式

    一.使用jndi的方式 这种方式方便测试人员不需要改代码,直接改变tomcat的server.xml就可以更改数据库连接 spring创建bean <bean id="dataSour ...

  5. Linux系统管理命令(1)accton的使用

    安装: apt install acct accton accton命令是Linux系统进程管理命令之一,它的作用是打开进程统计,如果不带任何参数,即关闭进程统计.         具体用法为:acc ...

  6. 深入浅出数据结构C语言版(22)——排序决策树与桶式排序

    在(17)中我们对排序算法进行了简单的分析,并得出了两个结论: 1.只进行相邻元素交换的排序算法时间复杂度为O(N2) 2.要想时间复杂度低于O(N2),算法必须进行远距离的元素交换 而今天,我们将对 ...

  7. 均值滤波去除图像噪声的matlab程序

    所谓均值滤波实际上就是用均值替代原图像中的各个像素值. 均值滤波的方法是:对待处理的当前像素,选择一个模板,该模板为其近邻的若干像素组成,用模板中的像素的均值来替代原像素. 优点:算法简单,计算速度快 ...

  8. 基于Quartz实现简单的定时发送邮件

    一.什么是Quartz Quartz 是一个轻量级任务调度框架,只需要做些简单的配置就可以使用:它可以支持持久化的任务存储,即使是任务中断或服务重启后,仍可以继续运行.Quartz既可以做为独立的应用 ...

  9. Net知识图谱

    对于Web系统开发来说,Net其实也是有好多知识点需要学的,虽然目前JAVA是主流,就业市场比较大,但Net也在积极的拥抱开源,大Net Core 2 出来了,这无疑给Net开发者带来更大的希望,好了 ...

  10. CodeForces 242E二维线段树

                                                                                           E. XOR on Seg ...