LeetCode——Lowest Common Ancestor of a Binary Search Tree
Description:
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.
给出一颗二叉查找树,两个节点,找出这两个节点的最近公共祖先。
思路:递归可以使这道题变得异常的简单。如果两个节点是在根节点两侧,那么最近的公共祖先就是这个根节点,如果是左侧(右侧),则把根节点递归到左子树(右子树)。
/**
* 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;
}
int max = p.val > q.val ? p.val : q.val;
int min = p.val < q.val ? p.val : q.val;
if(max < root.val) {
return lowestCommonAncestor(root.left, p, q);
}
else if(min > root.val) {
return lowestCommonAncestor(root.right, p, q);
}
else {
return root;
} }
}
LeetCode——Lowest Common Ancestor of a Binary Search Tree的更多相关文章
- [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 ...
 - LeetCode: Lowest Common Ancestor of a Binary Search Tree  解题报告
		
https://leetcode.com/submissions/detail/32662938/ Given a binary search tree (BST), find the lowest ...
 - [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 ...
 - Python3解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 ...
 - LeetCode Lowest Common Ancestor of a Binary Search Tree (LCA最近公共祖先)
		
题意: 给一棵二叉排序树,找p和q的LCA. 思路: 给的是BST(无相同节点),那么每个节点肯定大于左子树中的最大,小于右子树种的最小.根据这个特性,找LCA就简单多了. 分三种情况: (1)p和q ...
 - 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  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
		
235. Lowest Common Ancestor of a Binary Search Tree Easy Given a binary search tree (BST), find the ...
 
随机推荐
- DataGridView使用技巧四:删除行操作
			
一.无条件的删除行 默认时,DataGridView是允许用户进行行的删除操作,选中要删除的行,按Delete键可以删除,该操作没有任何提示(只是删除界面显示的数据,不会真实删除数据库中的数据).如果 ...
 - 7 款灵巧实用的 CSS3/jQuery 工具
			
作为 Web 前端开发者,应该对 jQuery 比较熟悉,对免费开源的 jQuery 也用的非常多.但是随着 CSS3 标准的诞生和发展,很多 jQuery 插件也都纷纷应用了 CSS3 新标准,也因 ...
 - LVS学习笔记及总结(思维导图版)
			
转自: http://www.07net01.com/2015/10/944377.html 下图是我在跟随马哥的脚步学习LVS过程中的学习笔记,以此为蓝本总结的,若有不足之处请谅解!
 - 修改 ueditor1_4_3-utf8-php 它的图片上传地址
			
本来是这样的 至少应该是在,myapp目录下吧,从myapp文件夹,我的网站才刚开始啊...你让我将来怎么设置?麻烦... 找到uploader的文件 $rootPath = $_SERVER['DO ...
 - 关于Unity中的屏幕适配
			
一.Game视图的屏幕分辨率可以先自定义添加,供以后选择,以下是手游经常用到的分辨率: 1.1136X640,iPhone5 2.1920X1080,横屏,主流游戏都是这个分辨率 3.1080X192 ...
 - 关于Struts2的界面的摆放
			
控件有的时候怎么放都感觉放不到自己想要的位置,这时候可以有这几个做法 1.用一个table标签来创建一个表格,再在表格里面一行一列地放,比较整齐 2.用表格,结果却发现有的控件位置莫名其妙,这时候就要 ...
 - FFmpeg在Android上的移植优化步骤
			
http://blog.csdn.net/feixiang_john/article/details/7894188 从事多媒体软件开发的人几乎没有不知道FFmpeg的,很多视频播放器都是基于FFmp ...
 - linux -- 注销,关机,重启
			
注销:logout Logout 注销是登陆的相对操作,登陆系统后,若要离开系统,用户只要直接下达logout命令即可: [root@localhost root]#logout Red Hat ...
 - CentOS安装Emacs文本编辑器
			
我这里安装的是:emacs.24.2 下载地址:http://ftp.gnu.org/pub/gnu/emacs/emacs-24.2.tar.gz 下载文件:emacs-24.2.tar.gz 步骤 ...
 - 算法--将Excel列索引转换成默认标识
			
使用POI导入Excel时,有时对模板进行验证,假如第1行第1列错误,此时的rowIndex=columnIndex=0,与平时看到的Excel行列标记相比不太直观,因此通过相应的算法将其转换成人们熟 ...