Leetcode_235_Lowest Common Ancestor of a Binary Search Tree
本文是在学习中的总结,欢迎转载但请注明出处:http://blog.csdn.net/pistolove/article/details/48392713
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.
思路:
(1)题意为给定一个BST以及该树上两节点,求这两节点的最近公共祖先。
(2)该题比较简单,通过递归很好实现。如果两节点分别分布在根节点的左右子树上,那么它们的最近公共祖先只能是树根;如果两节点同时分部在根节点的相同子树上,则需要分别递归进行判定。
(3)详情见下方代码。希望本文对你有所帮助。
算法代码实现如下:
package leetcode;
import leetcode.utils.TreeNode;
/**
*
* @author liqqc
*
*/
public class Lowest_Common_Ancestor_of_a_Binary_Search_Tree {
public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
// 在树的两边
if (p.val >= root.val && q.val <= root.val)
return root;
else if (p.val <= root.val && q.val >= root.val)
return root;
// 在树的一边 左边或右边
else if (p.val < root.val && q.val < root.val) {
// 左边
return lowestCommonAncestor(root.left, p, q);
}
else {
// 右边
return lowestCommonAncestor(root.right, p, q);
}
}
}
Leetcode_235_Lowest Common Ancestor of a Binary Search Tree的更多相关文章
- [geeksforgeeks] Lowest Common Ancestor in a Binary Search Tree.
http://www.geeksforgeeks.org/lowest-common-ancestor-in-a-binary-search-tree/ Lowest Common Ancestor ...
- 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 ...
- Lowest Common Ancestor of a Binary Search Tree、Lowest Common Ancestor of a Binary Search Tree
1.Lowest Common Ancestor of a Binary Search Tree Total Accepted: 42225 Total Submissions: 111243 Dif ...
- 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 ...
- 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_Lowest Common Ancestor of a Binary Search Tree (Binary Tree)
Lowest Common Ancestor of a Binary Search Tree 一.题目描写叙述 二.思路及代码 二叉搜索树有个性质:左子树的值都比根节点小,右子树的值比根节点大.那么我 ...
- LeetCode_235. Lowest Common Ancestor of a Binary Search Tree
235. Lowest Common Ancestor of a Binary Search Tree Easy Given a binary search tree (BST), find the ...
- 60-Lowest Common Ancestor of a Binary Search Tree
Lowest Common Ancestor of a Binary Search Tree My Submissions QuestionEditorial Solution Total Accep ...
随机推荐
- Android动态加载入坑指南
曾几何时,国内各大公司掀起了一股研究Android动态加载的技术,两年多过去了,动态加载技术俨然成了Android开发中必须掌握的技术.那么动态加载技术是什么呢,这里谈谈我的个人看法,如有雷同,纯属偶 ...
- H3C数据中心虚拟化解决方案技术白皮书
缩略语清单: 缩略语 英文全名 中文解释 IDC Internet Data Center 互联网数据中心 VRF Virtual Router Forwarding 虚拟路由器转发 SMP Symm ...
- Java进阶(四十)Java类、变量、方法修饰符讲解
Java进阶(四十)Java类.变量.方法修饰符讲解 Java类修饰符 abstract: 将一个类声明为抽象类,没有实现的方法,需要子类提供方法实现. final: 将一个类生命为最终(即非继承类) ...
- Tomcat内核之类加载器工厂
Java虚拟机利用类加载器将类载入内存,以供使用.在此过程中类加载器要做很多的事情,例如读取字节数组.验证.解析.初始化等.而Java提供的URLClassLoader类能方便地将jar.class或 ...
- C++对象模型的那些事儿之三:默认构造函数
前言 继前两篇总结了C++对象模型及其内存布局后,我们继续来探索一下C++对象的默认构造函数.对于C++的初学者来说,有如下两个误解: 任何class如果没有定义default constructor ...
- Java基础---Java---面试题---交通灯管理系统(面向对象、枚举)
交通灯管理系统的项目需求: 模拟实现十字路口的交通灯管理系统逻辑,具体需求如下: 1.异步随机生成按照各个路线行驶的车辆 例如: 由南向而来去往北向的车辆-----直行车辆 由西向而来去往南 ...
- 最简单的基于DirectShow的示例:视频播放器
===================================================== 最简单的基于DirectShow的示例文章列表: 最简单的基于DirectShow的示例:视 ...
- HMM:隐马尔可夫模型HMM
http://blog.csdn.net/pipisorry/article/details/50722178 隐马尔可夫模型 隐马尔可夫模型(Hidden Markov Model,HMM)是统计模 ...
- Spark编程模型
主要参考: Spark官方文档:http://spark.apache.org/docs/latest/programming-guide.html 炼数成金PPT:02Spark编程模型和解析 本文 ...
- web多语言url的设计
因为项目要支持国际化,最近跟一个同事在讨论多语言版本下面url如何设计,假如我们需要支持en和cn的版本. 他倾向于支持如下的url格式,后续以格式1指代: /en/group/abc.html /c ...