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 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.
由于这是一颗二叉搜索树,我们根据nodes v and w值的大小,能确定他们位于根节点的左边还是右边。如果两个节点都在根节点的左子树上,那么他们的LCA也肯定在根节点的左子树;如果两个节点都在根节点的右子树上,那么他们的LCA也肯定在根节点的右子树;如果两个节点分别位于根结的左右子树,那么根节点就是他们的LCA。否则的话直接返回根节点即可。代码如下:
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {
public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
if(root==null || p==null || q==null) return null; if(p.val<root.val && q.val<root.val){ //在根节点左边
return lowestCommonAncestor(root.left, p, q);
}
else if(p.val>root.val && q.val>root.val){ //在根节点右边
return lowestCommonAncestor(root.right, p, q);
}
else return root; //在根节点两侧,或者其中一个节点与根节点相等
}
}
LeetCode OJ 235. Lowest Common Ancestor of a Binary Search Tree的更多相关文章
- 【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
一天一道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 th ...
- 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 ...
- 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面试准备: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 二叉搜索树的最小共同父节点
Given a binary search tree (BST), find the lowest common ancestor (LCA) of two given nodes in the BS ...
随机推荐
- 字符串ID转换成字符串名字
select U.CnName+',' from f_splitstr('1828,1055333,1,1035681,752,494,22549,219,23860,478,23453,677, ...
- Date时间对象方法
- RabbitMQ持久化编码注意事项
以Java语言,MQ客户端为amqp-client作为示例 1.基本原则 direct模式,由生产者声明队列名,消费者也声明队列名 topic模式,由生产者声明交换器名,由消费者声明队列名+交换器名+ ...
- 火狐中,设置align="center"失效的解决方法
如下: <img align="center" style="vertical-align:top"></img> 只需要加上: ver ...
- PHP 时间与字符串的相互转化
1.求两个日期的差数,例如2007-3-5 ~ 2007-3-6 的日期差数 echo abs(strtotime("2007-3-5") - strtotime("20 ...
- mac下安装composer
打开命令后 cd /usr/local/bin 然后执行 curl -sS https://getcomposer.org/installer | php 接下来 sudo mv composer.p ...
- Python中的list,tuple,dict,set
list=[11,"aa",33] 增: list.insert(1,"asas") list.append(22) 删: list.pop() list.po ...
- 图片上传预览 支持html5的浏览器
<img src="" width="100px" height="100px"/> <input type=" ...
- OAuth流程
简介 OAuth是一种协议,OAuth协议为用户资源的授权提供了一个安全的.开放而又简易的标准 第三方若想访问用户资源,就必须遵守服务提供商实现的OAuth协议 OAuth授权的步骤(新浪微博为例) ...
- Openjudge-计算概论(A)-奇数单增序列
描述: 给定一个长度为N(不大于500)的正整数序列,请将其中的所有奇数取出,并按升序输出. 输入共2行:第1行为 N:第2行为 N 个正整数,其间用空格间隔.输出增序输出的奇数序列,数据之间以逗号间 ...