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 ...
随机推荐
- 翻译:last_value()函数(已提交到MariaDB官方手册)
本文为mariadb官方手册:LAST_VALUE()的译文. 原文:https://mariadb.com/kb/en/last_value/我提交到MariaDB官方手册的译文:https://m ...
- μC/OS-II 任务的同步与通信 --- 信号量
任务间通信 系统中的多个任务在运行时,经常需要互相无冲突地访问同一个共享资源,或者需要互相支持和依赖,甚至有时还要互相加以必要的限制和制约,才保证任务的顺利运行.因此,操作系统必须具有对任务的运行进行 ...
- WPF 实现ScrollViewer的垂直偏移滚动跳转
问题:考虑屏幕大小,一般都是会在表单问卷的页面使用ScrollViewer.问卷中问题漏填漏选时,在提交时校验不过,需要滚动跳转至漏填漏选项. 页面如下: 每个选项使用StackPanel,并对复选框 ...
- [转]Angular开发(十八)-路由的基本认识
angular router https://angular.io/guide/router 本文转自:https://blog.csdn.net/kuangshp128/article/detail ...
- C# 读写App.config配置文件
一.C#项目中添加App.config配置文件 在控制台程序中,默认会有一个App.config配置文件,如果不小心删除掉,或者其他程序需要配置文件,可以通过添加得到. 添加步骤:右键项目名称,选择“ ...
- mysql存储过程调用含out参数
mysql 数据库有以下存储过程: CREATE DEFINER=`root`@`localhost` PROCEDURE `hovertreeTest`( IN `Param1` INT, ), O ...
- JavaScript有这几种测试分类
译者按: 也许你讨厌测试,但是你不得不面对它,所以至少区分一下单元测试.集成测试与功能测试?对吧… 原文: What are Unit Testing, Integration Testing and ...
- ES6 import and export
定义: 用来导入或者导出模块. ES6 的模块自动采用严格模式,不管你有没有在模块头部加上"use strict";. 严格模式主要有以下限制. 变量必须声明后再使用 函数的参数不 ...
- 使用JavaScript获取URL中的参数(两种方法)
本文给大家分享两种方法使用js获取url中的参数,其中方法二是使用的正则表达式方法,大家可以根据需要选择比较好的方法,废话不多说了,直接看详细介绍吧. 方法一: //取url参数 var type = ...
- js正整数正则表达式
function testNumber(){ var yourinputValue=$("#yourinputId").val(); var reg = /^[1-9]\d*$/; ...