这是悦乐书的第197次更新,第203篇原创

01 看题和准备

今天介绍的是LeetCode算法题中Easy级别的第59题(顺位题号是235)。给定二叉搜索树(BST),找到BST中两个给定节点的最低共同祖先(LCA)。根据维基百科上LCA的定义:“最低共同祖先在两个节点p和q之间定义为T中的最低节点,其中p和q都是后代(我们允许节点成为其自身的后代)。”

给定二叉搜索树:root = [6,2,8,0,4,7,9,null,null,3,5]

            6
/ \
2 8
/ \ / \
0 4 7 9
/ \
3 5

例如:

输入:root = [6,2,8,0,4,7,9,null,null,3,5],p = 2,q = 8

输出:6

说明:节点2和8的LCA为6。



输入:root = [6,2,8,0,4,7,9,null,null,3,5],p = 2,q = 4

输出:2

说明:节点2和4的LCA是2,因为节点可以是其自身的后代,根据LCA的定义。

注意:

  • 所有节点的值都是唯一的。

  • p和q不同,两个值都将存在于BST中。

本次解题使用的开发工具是eclipse,jdk使用的版本是1.8,环境是win7 64位系统,使用Java语言编写和测试。

02 第一种解法

利用迭代的方法。二叉搜索树的特点是 左节点的值 < 根节点的值 < 右节点的值,如果p和q的节点值都小于当前节点,此时指针应该移动到当前节点的左节点,再去比较。如果p和q的节点值都大于当前节点,此时指针应该移动到当前节点的右节点,再去比较。否则就返回当前节点。

public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
while (root != null) {
if (p.val < root.val && q.val < root.val) {
root = root.left;
} else if (p.val > root.val && q.val > root.val) {
root = root.right;
} else {
return root;
}
}
return root;
}

03 第二种解法

利用递归的方法,判断逻辑和迭代的解法一样。如果当前节点的值比p和q中最大值都要大,因为当前节点的左子节点值肯定小于当前节点的值,所以要往当前节点的左子节点方向移动。如果当前节点的值比p和q中最小值都要小,因为当前节点的右子节点值肯定大于当前节点的值,所以要往当前节点的右子节点方向移动。然后再去判断。

public TreeNode lowestCommonAncestor2(TreeNode root, TreeNode p, TreeNode q) {
if (root == null) {
return null;
}
if (root.val > Math.max(p.val, q.val)) {
return lowestCommonAncestor2(root.left, p, q);
}
if (root.val < Math.min(p.val, q.val)) {
return lowestCommonAncestor2(root.right, p, q);
}
return root;
}

04 小结

算法专题目前已连续日更超过一个月,算法题文章59+篇,公众号对话框回复【数据结构与算法】、【算法】、【数据结构】中的任一关键词,获取系列文章合集。

以上就是全部内容,如果大家有什么好的解法思路、建议或者其他问题,可以下方留言交流,点赞、留言、转发就是对我最大的回报和支持!

LeetCode算法题-Lowest Common Ancestor of a Binary Search Tree的更多相关文章

  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 解题报告(Java & Python)

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

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

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

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

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

  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. shell脚本动画小工具

    shell脚本动画小工具 看gif图: shell脚本版 脚本内容如下: #!/usr/bin/env bash ## ---------------------------------------- ...

  2. 一个小时学会Maven

    一.为什么要Maven 在开发中经常需要依赖第三方的包,包与包之间存在依赖关系,版本间还有兼容性问题,有时还里要将旧的包升级或降级,当项目复杂到一定程度时包管理变得非常重要. Maven是当前最受欢迎 ...

  3. sql-索引的作用(超详细)

    (一)深入浅出理解索引结构 实际上,您可以把索引理解为一种特殊的目录.微软的SQL SERVER提供了两种索引:聚集索引(clustered index,也称聚类索引.簇集索引)和非聚集索引(nonc ...

  4. T-SQL基础(三)之子查询与表表达式

    子查询 在嵌套查询中,最外面查询结果集返回给调用方,称为外部查询.嵌套在外部查询内的查询称为子查询,子查询的结果集供外部查询使用. 根据是否依赖外部查询,可将子查询分为自包含子查询和相关子查询.自包含 ...

  5. c# 服务

    注:服务里的timer System.Timers.Timer time=new System.Timers.Timer();  time.Interval = 3000;  //设置计时器事件间隔执 ...

  6. C# Winform开发以及控件开发的需要注意的,被人问怕了,都是基础常识

    我是搞控件开发的,经常被人问,所以把一些问题记录了下来!如果有人再问,直接把地址丢给他看. 一. 经常会有人抱怨Winform界面闪烁,下面有几个方法可以尽可能的避免出现闪烁 1.控件的使用尽量以纯色 ...

  7. 【Java每日一题】20170119

    20170118问题解析请点击今日问题下方的“[Java每日一题]20170119”查看(问题解析在公众号首发,公众号ID:weknow619) package Jan2017; import jav ...

  8. Add Again(重复元素排序) UVA11076

    Add Again Summation of sequence of integers is always a common problem in Computer Science. Rather t ...

  9. 快速掌握JavaScript面试基础知识(三)

    译者按: 总结了大量JavaScript基本知识点,很有用! 原文: The Definitive JavaScript Handbook for your next developer interv ...

  10. 学用纯CSS3打造可折叠树状菜单

    CSS执行顺序与优先权的问题其实就是一个冲突解决的问题,当同一个元素(或内容)被CSS选择符选中时,就要按照优先权取舍不同的CSS规则,这其中涉及到的问题其实很多.首先就是CSS规则的specific ...