LeetCode算法题-Lowest Common Ancestor of a Binary Search Tree
这是悦乐书的第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的更多相关文章
- 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 ...
- 【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 ...
- 【LeetCode】235. Lowest Common Ancestor of a Binary Search Tree 解题报告(Java & Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 [LeetCode] https://leet ...
- 【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 ...
- 【一天一道LeetCode】#235. Lowest Common Ancestor of a Binary Search Tree
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...
- 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 ...
- 【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 ...
- 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 ...
- 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 ...
随机推荐
- 使用Task异步执行方法_多线程_应用程序池
偶然遇到在执行登录的方法需要发送消息队列导致登录时间过长的问题,从网上查了一些方法,先将一个简单的异步处理程序的小例子展示出来,供大家参考: 备注:该方法是从应用程序程序所在的线程池中获取线程,第一次 ...
- 【转载】在Centos系统上采用二进制文件部署Node.js环境
Node.js 是一个基于 Chrome V8 引擎的 JavaScript 运行环境,用来方便地搭建快速的易于扩展的网络应用.Node.js 使用了一个事件驱动.非阻塞式 I/O 的模型,使其轻量又 ...
- vb.net 使用NPO自定義格式
'導入命名空間 Imports System.IOImports NPOI.HSSF.UserModelImports NPOI.HPSFImports NPOI.POIFS.FileSystem P ...
- python基础学习(十一)公共方法
Python内置函数 Python 包含了以下内置函数: 切片 切片 使用 索引值 来限定范围,从一个大的 字符串 中 切出 小的 字符串 列表 和 元组 都是 有序 的集合,都能够 通过索引值 获取 ...
- youtube link for review STAT(1)
Confidence Interval: https://www.youtube.com/watch?v=bekNKJoxYbQ Introduction to confidence interval ...
- C# Dev XtraReport 简单测试
using System; using System.Collections.Generic; using System.Linq; using System.Text; using DevExpre ...
- html/css更改子级继承的父级属性
一个精美的网页需要的样式很多,在父级上设置的字体颜色或者大小,在其子元素中不一定全部相同,这时候要更改其中某一项的样式怎么办呢. 很多新手朋友就不明白,会迷惑为什么我使用class单独命名了,重新设置 ...
- java读写properties工具代码
package test612; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundExc ...
- 你不可不知的Java引用类型之——ReferenceQueue源码详解
定义 ReferenceQueue是引用队列,用于存放待回收的引用对象. 说明 对于软引用.弱引用和虚引用,如果我们希望当一个对象被垃圾回收器回收时能得到通知,进行额外的处理,这时候就需要使用到引用队 ...
- (网页)parseFloat在工作中遇到的错误
parseFloat(23.23) == parseFloat(23.2299999999999).toFixed(2) 类似与以上的代码,展示没什么问题,一旦比较,看展示相等其实不相等,小数位的问题 ...