https://leetcode.com/submissions/detail/32662938/

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.

Leetcode又出新题啦

SOLUTION 1:

使用递归可以轻松解决此问题。对于此题我们可以分为三种情况讨论:

1. P, Q都比root小,则LCA在左树,我们继续在左树中寻找LCA

2. P, Q都比root大,则LCA在右树,我们继续在右树中寻找LCA

3. 其它情况,表示P,Q在root两边,或者二者其一是root,或者都是root,这些情况表示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) {
// 1439.
// 1. Two nodes are on left, go left.
// 2. Two nodes are on right, go right.
// 3. They are in both sides, return root.
// if root == null, return null.
if (root == 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: Lowest Common Ancestor of a Binary Search Tree 解题报告的更多相关文章

  1. 【LeetCode】235. Lowest Common Ancestor of a Binary Search Tree 解题报告(Java & Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 [LeetCode] https://leet ...

  2. [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 ...

  3. [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 ...

  4. 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 no ...

  5. 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 ...

  6. LeetCode Lowest Common Ancestor of a Binary Search Tree (LCA最近公共祖先)

    题意: 给一棵二叉排序树,找p和q的LCA. 思路: 给的是BST(无相同节点),那么每个节点肯定大于左子树中的最大,小于右子树种的最小.根据这个特性,找LCA就简单多了. 分三种情况: (1)p和q ...

  7. 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 ...

  8. 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 利用二 ...

  9. 【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 ...

随机推荐

  1. 产品中 configure/cross compile的一个bug

    在mac机上, 为iPhone版本编译产品. 运行./configure报错如下: configure:22793: error: cannot run test program while cros ...

  2. 一些常用的Bootstrap模板资源站

    2013-11-13 23:28:09   超级Bootstrap模板库:http://www.wrapbootstrap.com/ 免费的HTML5 响应式网页模板:http://html5up.n ...

  3. JBoss 系列四十八:JBoss 7/WildFly 使用TCP构建集群

    我知道JBoss 集群Default 的设定就是UDP(JGroups),但在实际环境中的网络环境时常不允许UDP,在这种情况下,我们就需要使用TCP. JBoss 7/WildFly 中负责集群的主 ...

  4. elixir 入门笔记

    安装 MAC 平台用 brew 安装 brew update brew install elixir 如果没有 erlang 环境,上面的命令会自定安装 erlang 的环境. 基本数据类型 iex& ...

  5. CSDN 分糖果算法的思路和求助

    昨天晚上 在csdn上做了一道分糖果的题目,我自个测的是没有问题,但是提交答案后,老失败,提示 你的程序正常运行并输出了结果,但是答案错误你的程序输出结果与测试数据中的输出结果不符 我先把自个思路说一 ...

  6. 通信服务器群集——跨服务器通信Demo(源码)

    对于一些基于TCP Socket的大型C/S应用来说,能进行跨服务器通信可能是一个绕不开的功能性需求.出现这种需求的场景类似于下面描述的这种情况. 假设,我们一台TCP应用服务器能同时承载10000人 ...

  7. 微软BI 之SSIS 系列 - MVP 们也不解的 Scrip Task 脚本任务中的一个 Bug

    开篇介绍 前些天自己在整理 SSIS 2012 资料的时候发现了一个功能设计上的疑似Bug,在 Script Task 中是可以给只读列表中的变量赋值.我记得以前在 2008 的版本中为了弄明白这个配 ...

  8. Unity开发游戏 flapybird 无广告老马版分享

    Flapybird确实是一款非常好玩的游戏,但是上手难度比较大.经过老马模仿加工,把游戏难度降低,而且不加入任何广告. 特此分享.下载地址:http://files.cnblogs.com/fly_d ...

  9. 高德地图iOS SDK限制地图的缩放比例

    问题 高德地图的iOS SDK 3D版中(v2.4.0), 显示范围在560m左右时建筑会呈现3D效果. 我们有没有办法可以限制地图最小缩放到这个比例, 从而保证建筑始终使用3D效果显示呢? 探索 高 ...

  10. 免费好用的web应用托管平台-续

    上一篇博客给大家推荐了目前处于免费阶段的PAAS平台,可以托管各种应用,大家反响很不错,说明大家还是很需要和认可这个免费托管各种web应用的京东云擎平台.但是很多用户还是很担心未来可能还是会收费,对于 ...