Java [Leetcode 235]Lowest Common Ancestor of a Binary Search Tree

时间:2022-09-02 08:15:26

题目描述:

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.

解题思路:

对于p,q和root。如果p和q都比root小,那么LCA必定在root的左子树上面;如果p和q都比root大,那么LCA必定在右子树上面;如果一大一小,那么root即为该值。

代码如下:

/**
* 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(Math.max(p.val, q.val) < root.val)
return lowestCommonAncestor(root.left, p, q);
else if(Math.min(p.val, q.val) > root.val)
return lowestCommonAncestor(root.right, p, q);
else
return root;
}
}

  

Java [Leetcode 235]Lowest Common Ancestor of a Binary Search Tree的更多相关文章

  1. leetcode 235&period; Lowest Common Ancestor of a Binary Search Tree 236&period; Lowest Common Ancestor of a Binary Tree

    https://www.cnblogs.com/grandyang/p/4641968.html http://www.cnblogs.com/grandyang/p/4640572.html 利用二 ...

  2. &lbrack;LeetCode&rsqb; 235&period; 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 ...

  3. &lbrack;LeetCode&rsqb; 235&period; 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 ...

  4. LeetCode 235&period; 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 ...

  5. leetcode 235&period; 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 ...

  6. &lpar;easy&rpar;LeetCode 235&period;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 ...

  7. Java for LeetCode 235 Lowest Common Ancestor of a Binary Search Tree

    递归实现如下: public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) { if(p.val>ro ...

  8. Leetcode 235 Lowest Common Ancestor of a Binary Search Tree 二叉树

    给定一个二叉搜索树的两个节点,找出他们的最近公共祖先,如, _______6______ / \ ___2__ ___8__ / \ / \ 0 4 7 9 / \ 3 5 2和8的最近公共祖先是6, ...

  9. 【LeetCode】235&period; Lowest Common Ancestor of a Binary Search Tree &lpar;2 solutions&rpar;

    Lowest Common Ancestor of a Binary Search Tree Given a binary search tree (BST), find the lowest com ...

随机推荐

  1. CSS3 background-image背景图片相关介绍

    这里将会介绍如何通过background-image设置背景图片,以及背景图片的平铺.拉伸.偏移.设置大小等操作. 1. 背景图片样式分类 CSS中设置元素背景图片及其背景图片样式的属性主要以下几个: ...

  2. 在web中使用windows控件,实现摄像头功能

    最近做的一个Web版的视频会议项目,需要在网页中播放来自远程摄像头采集的实时视频,我们已经有了播放远程实时视频的使用C#编写的windows控件,如何将其嵌入到网页中去了?这需要使用一种古老的技术,A ...

  3. PE文件格式 持续更新ing

    PE文件就是exe文件和dll文件,前者是可执行文件,后者是动态连接库文件.两者的区别仅仅是字面上的,唯一的区别就是内部的一个字段标识这个文件是exe文件还是dll文件. 对于PE文件格式,举一个例子 ...

  4. JS的强大

    JS很强大,对于网页设计者来说,会用JS真的很重要. 学好我的linux,和数据结构.

  5. 多线程编程之Linux环境下的多线程(一)

    一.Linux环境下的线程 相对于其他操作系统,Linux系统内核只提供了轻量级进程的支持,并未实现线程模型.Linux是一种“多进程单线程”的操作系统,Linux本身只有进程的概念,而其所谓的“线程 ...

  6. bzoj1499&colon; &lbrack;NOI2005&rsqb;瑰丽华尔兹

    dp. 首先我们可以看到每个时间段只能往一个方向转移最多t步(t为时间段的长度),所以我们可以按时间段dp.因为这个前后值互不影响,也不用占用这一维空间就可以省去. 然后每个时间段内是一列一列(行) ...

  7. 【原】web服务器占有量统计等 web网站

    根据W3Techs的统计,目前世界排名(根据Alexa)前100万的网站中 1. https://w3techs.com/ nginx 中文站 2. http://www.nginx.cn/doc/

  8. 概率图模型之有向图与无向图之间的关系 I map D map perfect map(完美图) 概念

    我们已经讨论了有向图和无向图框架下的概率模型,那么我们有必要讨论一下它们二者的关系.

  9. eclipse中新建maven项目

    maven是个项目管理工具,集各种功能于一身,下面介绍maven web项目在eclipse种的配置,并于tomcat集成.配置成功后,可以跟一般的web项目一样调试. 一.准备条件 1.安装下载jd ...

  10. OpenCV 学习笔记 04 深度估计与分割——GrabCut算法与分水岭算法

    1 使用普通摄像头进行深度估计 1.1 深度估计原理 这里会用到几何学中的极几何(Epipolar Geometry),它属于立体视觉(stereo vision)几何学,立体视觉是计算机视觉的一个分 ...