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 ...
随机推荐
- httpd htpasswd命令
apache httpd系列文章:http://www.cnblogs.com/f-ck-need-u/p/7576137.html htpasswd用于为指定用户生成基于网页用户身份认证的密码,由h ...
- 华为路由器 IPSec 与 GRE 结合实验
二者结合的目的 GRE 支持单播.组播.广播,IPSec 仅支持单播.GRE 不支持对于数据完整性以及身份认证的验证功能,并且也不具备数据加密保护.而 IPSec 恰恰拥有强大的安全机制.达到了互补的 ...
- [转]Laravel - Where null and Where not null eloquent query example
本文转自: https://hdtuto.com/article/laravel-where-null-and-where-not-null-eloquent-query-example- if yo ...
- [转]如何将高版本的SQL Server数据库备份到低版本的SQL Server
本文转自:https://blog.csdn.net/wang465745776/article/details/54969676 前提条件备份SQL Server服务器版本为:12.0.2000.8 ...
- [PHP] 算法-将一个字符串转换成一个整数的PHP实现
题目描述 将一个字符串转换成一个整数(实现Integer.valueOf(string)的功能,但是string不符合数字要求时返回0),要求不能使用字符串转换整数的库函数. 数值为0或者字符串不是一 ...
- Python 集合符号
& 求交集 l 求并集 ^ 交叉补集 - 求差集 > = < =
- 汇编语言--微机CPU的指令系统(五)(移位操作指令)
(5) 移位操作指令 移位操作指令是一组经常使用的指令,它包括算术移位.逻辑移位.双精度移位.循环移位和带进位的循环移位等五大类. 移位指令都有指定移动二进制位数的操作数,该操作数可以是立即数或CL的 ...
- 向后台提交数据:cookie,secure_cookie,
向后台提交数据除了前端url,form表单,Ajax外还可以用cookie,secure_cookie,提交更多信息可以在用cookie基础上用session, cookie,secure_cooki ...
- Python入门基础之迭代和列表生成式
什么是迭代 在Python中,如果给定一个list或tuple,我们可以通过for循环来遍历这个list或tuple,这种遍历我们成为迭代(Iteration). 在Python中,迭代是通过 for ...
- iOS----------被拒原因
推荐地址:https://developer.apple.com/app-store/review/guidelines/cn/ 目录 1.条款与条件 2.功能 3.元数据 4.位置 5.推送通知 6 ...