LeetCode (236):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.
解题思路:
节点a与节点b的公共祖先c一定满足:a与b分别出现在c的左右子树上(如果a或者b本身不是祖先的话)。
Java代码:
class TreeNode {
int val;
TreeNode left;
TreeNode right;
TreeNode(int x) { val = x; }
}
public class Solution {
public static TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
if(root==null) return null;
if(root==p||root==q) return root;
TreeNode L=lowestCommonAncestor(root.left,p,q);
TreeNode R=lowestCommonAncestor(root.right,p,q);
if(L!=null&&R!=null) return root;
return L!=null?L:R;
}
//test
public static void main(String[] args) {
// TODO Auto-generated method stub
TreeNode tr=new TreeNode(1);
TreeNode nodeB=new TreeNode(2);
TreeNode nodeC=new TreeNode(3);
TreeNode nodeD=new TreeNode(4);
TreeNode nodeE=new TreeNode(5);
tr.left=nodeB;
tr.right=nodeC;
tr.left.right=nodeD;
tr.left.left=nodeE;
System.out.print(lowestCommonAncestor(tr, nodeC, nodeE).val);
}
}
LeetCode (236):Lowest Common Ancestor of a Binary Search Tree的更多相关文章
- 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 二叉搜索树的最近公共祖先
Given a binary search tree (BST), find the lowest common ancestor (LCA) of two given nodes in the BS ...
- 235.236. Lowest Common Ancestor of a Binary (Search) Tree -- 最近公共祖先
235. Lowest Common Ancestor of a Binary Search Tree Given a binary search tree (BST), find the lowes ...
- [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 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 ...
- leetcode: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 [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 ...
随机推荐
- Java基础之Calendar类、JNDI之XML
一.Calendar类 从JDK1.1版本开始,在处理日期和时间时,系统推荐使用Calendar类进行实现.在设计上,Calendar类的功能要比Date类强大很多,而且在实现方式上也比Date类要 ...
- CH5101 LCIS【线性dp】
5101 LCIS 0x50「动态规划」例题 描述 熊大妈的奶牛在小沐沐的熏陶下开始研究信息题目.小沐沐先让奶牛研究了最长上升子序列,再让他们研究了最长公共子序列,现在又让他们研究最长公共上升子序列了 ...
- IDEA安装Python环境,并加入Anaconda环境
为什么做这个事情? 1.首先,Anaconda中已经有各种科学计算环境,包括后面安装的tensorflow 2.通过IDEA中配置就达到了Scala.Python.Java同时运行的目的. Intel ...
- linux下安装mysql-5.7.25
1.下载对应安装包 https://dev.mysql.com/downloads/mysql/ 2.卸载旧版本mysql 列出旧版本MySql的组件列表 rpm -qa | grep mysql ...
- Network Security Services If you want to add support for SSL, S/MIME, or other Internet security standards to your application, you can use Network Security Services (NSS) to implement all your securi
Network Security Services | MDN https://developer.mozilla.org/zh-CN/docs/NSS 网络安全服务 (NSS) 是一组旨在支持支持安 ...
- centos 7.5 最小化安装
参考:https://www.tecmint.com/centos-7-installation/ ================================================== ...
- Centos locate 文件搜索命令(十一)
locate命令 locate 文件名 在后台数据库中按文件名搜索,搜索速度更快 /var/lib/mlocate #locate命令所搜索的后台数据库 updatedb 更新数据库 locate搜索 ...
- 剑指Offer——和为S的连续正数序列
题目描述: 小明很喜欢数学,有一天他在做数学作业时,要求计算出9~16的和,他马上就写出了正确答案是100.但是他并不满足于此,他在想究竟有多少种连续的正数序列的和为100(至少包括两个数).没多久, ...
- iOS开发- 速学Swift-中文概述
Swift是什么? Swift是苹果于WWDC 2014公布的编程语言,这里引用The Swift Programming Language的原话: Swift is a new programmin ...
- 怎样将Arranged_2压入General_Polygon_set_2中
Thursday, March 14, 2013 How to Jam an Arrangement_2 into a General_polygon_set_2 I spent about thre ...