题目描述:

一个二叉搜索树,给定两个节点a,b,求最小的公共祖先

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

例如:

2,8 —->6 2,4—–>2

原文描述:

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 < = root <= b,可以确定root是lct
  • 采用二分搜索的方式递归,root > a,b,继续遍历左子树,反之右边的子树

    -在递归的开始判断空值的情况,直接返回root

代码:

/**
 * 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 || p == null || q == null)
            return root;
        if(p.val > root.val && q.val > root.val)
            return lowestCommonAncestor(root.right, p, q);
        else if(p.val < root.val && q.val < root.val)
            return lowestCommonAncestor(root.left, p, q);
        else
            return root;
    }
}

更多的leetcode的经典算法,查看我的leetcode专栏,链接如下:

leetcode专栏

我的微信二维码如下,欢迎交流讨论

欢迎关注《IT面试题汇总》微信订阅号。每天推送经典面试题和面试心得技巧,都是干货!

微信订阅号二维码如下:

72【leetcode】经典算法- Lowest Common Ancestor of a Binary Search Tree(lct of bst)的更多相关文章

  1. leetcode面试准备:Lowest Common Ancestor of a Binary Search Tree & Binary Tree

    leetcode面试准备:Lowest Common Ancestor of a Binary Search Tree & Binary Tree 1 题目 Binary Search Tre ...

  2. 【LeetCode】235. Lowest Common Ancestor of a Binary Search Tree (2 solutions)

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

  3. 【一天一道LeetCode】#235. Lowest Common Ancestor of a Binary Search Tree

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...

  4. 【LeetCode】235. Lowest Common Ancestor of a Binary Search Tree 解题报告(Java & Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 [LeetCode] https://leet ...

  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. LeetCode OJ 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】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 th ...

  9. LeetCode OJ: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. SUSE10的虚拟机安装以及ORACLE 11g的安装

    SUSE10虚拟机安装与ORACLE安装 作者:张欣橙 本文所需要的所有参数均位于文末附录中 一.SUSE10虚拟机的安装与创建 新建虚拟机安装 选择下一步 选择下一步 选择下一步 选择下一步 选择下 ...

  2. 读书学习-Python--描述符(python3)

    转自我的知乎文章(https://zhuanlan.zhihu.com/p/32487852) 何为描述符?描述符就是实现了__get__.__set__和__delete__三者中任意一个的类.是用 ...

  3. iis部署python运行环境

    IIS部署 1.启用或者关闭windows功能,选择安装CGI,我这里已经安装过了. 2.安装后重新打开IIS看到CGI 3.配置ISAPI和CGI限制 4.右上角添加,路径是python安装路径,注 ...

  4. 剑指架构师系列-MySQL常用SQL语句

    (1)分清HAVING与WHERE的区别: HAVING 子句使你能够指定过滤条件,从而控制查询结果中哪些组可以出现在最终结果里面.WHERE 子句对被选择的列施加条件,而 HAVING 子句则对 G ...

  5. 剑指架构师系列-持续集成之Maven实现项目的编译、发布和部署

    Maven组织项目进行编译.部署 Maven项目基本的结构说明如下: mazhi  // 控制所有荐的编译.部署.发布 mazhi-app-parent  // 项目的父项目,有一些公共的设置可以被子 ...

  6. Java对象的内存布局以及对象所需内存大小计算详解

    1. 内存布局 在HotSpot虚拟机中,对象的内存布局可以分为三部分:对象头(Header). 实例数据(Instance Data)和对齐填充(Padding). 1) 对象头(Header): ...

  7. SpringMVC+Spring+Mybatis整合,使用druid连接池,声明式事务,maven配置

    一直对springmvc和mybatis挺怀念的,最近想自己再搭建下框架,然后写点什么. 暂时没有整合缓存,druid也没有做ip地址的过滤.Spring的AOP简单配置了下,也还没具体弄,不知道能不 ...

  8. Python 2.7 闭包的局限

    想法源自:http://stackoverflow.com/questions/141642/what-limitations-have-closures-in-python-compared-to- ...

  9. 安卓高级8 SurfaceView (1)

    文章转载:http://www.cnblogs.com/xuling/archive/2011/06/06/android.html 首先我们先来看下官方API对SurfaceView的介绍 Surf ...

  10. oracle手工生成AWR报告方法记录

    AWR(Automatic Workload Repository)报告是我们进行日常数据库性能评定.问题SQL发现的重要手段.熟练掌握AWR报告,是做好开发.运维DBA工作的重要基本功. AWR报告 ...