235. Lowest Common Ancestor of a Binary Search Tree(LCA最低公共祖先)
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.
思路:因为是bst, 如果root,的值介于pq之间,root就是共同祖先。
root不是共同祖先,则递归地判断左右子树是不是公共祖先。
递归版:
class Solution {
public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
if((root.val-p.val)*(root.val-q.val)<=0) return root;
return lowestCommonAncestor((root.val-p.val)>0?root.left:root.right, p, q);
}
}
循环版:
class Solution {
public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
while((root.val-p.val)*(root.val-q.val)>0)
root = ((root.val-p.val)>0?root.left:root.right);
return root; }
}
235. Lowest Common Ancestor of a Binary Search Tree(LCA最低公共祖先)的更多相关文章
- leetcode 235. Lowest Common Ancestor of a Binary Search Tree 236. Lowest Common Ancestor of a Binary Tree
https://www.cnblogs.com/grandyang/p/4641968.html http://www.cnblogs.com/grandyang/p/4640572.html 利用二 ...
- 【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 二叉搜索树的最小共同父节点
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 ...
- [刷题] 235 Lowest Common Ancestor of a Binary Search Tree
要求 给定一棵二分搜索树和两个节点,寻找这两个节点的最近公共祖先 示例 2和8的最近公共祖先是6 2和4的最近公共祖先是2 思路 p q<node node<p q p<=node& ...
- 【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 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 ...
- (easy)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 ...
随机推荐
- JAVA 并发编程-多个线程之间共享数据(六)
多线程共享数据的方式: 1.假设每一个线程运行的代码同样.能够使用同一个Runnable对象,这个Runnable对象中有那个共享数据,比如,卖票系统就能够这么做. 2,假设每一个线程运行的代码不同. ...
- JQuery------实现点击左右按钮,切换图片功能
如图: 代码: html @*商品主图片*@ <div class="product-img" style="display:table-cell;width:40 ...
- 第二十五篇:使用 sigaction 函数实现可靠信号
前言 在前文中,讲述了一个可靠信号的示例.它分成几个步骤组成( 请参考前文 ).在 Linux 系统编程中,有个方法可以将这些步骤给集成起来,让我们使用起来更加的方便. 那就是调用 sigaction ...
- Tiled地图编辑软件
Tiled官网: http://www.mapeditor.org/ 一个比较简单好用的地图编辑软件.百度可以找到很多教程. 在Egret中,官网提供了Tiled的工具类,但是教程和文档极少...只能 ...
- 【IIS】IIS 7.0/7.5 无法启动 w3svc 服务
一般情况下,window IIS安装完毕后,会启动C:\inetpub\ 产生 类似C:\inetpub\temp\apppools的文件夹,如果IIS被改动过,此文件夹不会自动生成.需要手动添加. ...
- ECharts使用(1)(转载)
转载自http://www.cnblogs.com/Olive116/p/3634480.html 1. EChart最新的文档目录. 首先创建一个解决方案,目录如下: 之前的一篇文章中讲到如果要使 ...
- 160506、Spring mvc新手入门(11)-返回json 字符串的其他方式
Spring MVC返回 json字符串的方式有很多种方法,这里介绍最简单,也是最常使用的两种方式 一.使用 PrintWriter printWriter 直接输出字符串到返回结果中 不需 ...
- Hadoop单点伪分布模式安装
Hadoop单点伪分布模式安装 概述 单点 single-node,单节点,即一台计算机. 伪分布式模式 pseudo-distributed mode 所谓集群,表面上看是多台计算机联合完成任务:但 ...
- hibernate中持久化对象的状态
持久化对象有以下几种状态: 临时对象(Transient): 在使用代理主键的情况下, OID 通常为 null 不处于 Session 的缓存中 在数据库中没有对应的记录 持久化对象(也叫”托管 ...
- 在R语言中封装类读写sql操作工具类
1.mysql_helper.R # 使用RMySQL操作数据库 # 载入DBI和RMySQL包 library(DBI) library(RMySQL) mysql_con <- functi ...