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.

由于这是一颗二叉搜索树,我们根据nodes v and w值的大小,能确定他们位于根节点的左边还是右边。如果两个节点都在根节点的左子树上,那么他们的LCA也肯定在根节点的左子树;如果两个节点都在根节点的右子树上,那么他们的LCA也肯定在根节点的右子树;如果两个节点分别位于根结的左右子树,那么根节点就是他们的LCA。否则的话直接返回根节点即可。代码如下:

 /**
* 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(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 OJ 235. Lowest Common Ancestor of a Binary Search Tree的更多相关文章

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

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

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

  3. 【一天一道LeetCode】#235. Lowest Common Ancestor of a Binary Search Tree

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...

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

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

  6. LeetCode OJ: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. 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 利用二 ...

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

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

随机推荐

  1. PHP中date函数月和日带0问题

    一.带零 echo date('Y-m-d'); 2012-08-08 二.不带零 echo date('Y-n-j'); 2012-8-8   以下为参数详解(转载): a - "am&q ...

  2. Java 服务器端手机验证码sdk

    感谢巨人们,站在巨人的肩膀上. 参考: http://blog.sina.com.cn/s/blog_80a6423d0102wm74.html#commonComment 1 点击生成验证码,发送到 ...

  3. 添加JUnit到Java Build Path

    1.第一种 新建项目,点击右键,选择properties->Java Build Path->Libraries->add library->JUnit->JUnit4- ...

  4. openstack私有云布署实践【15 创建租户网络+实例】

    这里以办公网测试环境为例,   (一)创建租户demo的网络   使用admin用户 source admin-openrc.sh 创建public公网 neutron net-create 1040 ...

  5. SQL优化及注意事项

    1. 把数据.日志.索引放到不同的I/O设备上,增加读取速度.数据量(尺寸)越大,提高I/O越重要. 2. 纵向.横向分割表,减少表的尺寸,如:可以把大数据量的字段拆分表. 3. 根据查询条件,建立索 ...

  6. git的使用,将本地项目push到github上

    Git教程(推荐): http://www.liaoxuefeng.com/wiki/0013739516305929606dd18361248578c67b8067c8c017b000git是先用a ...

  7. 【成长之路】【python】python基础2

    1.位运算 &(与) | (或) ~ (非) ^(异或) 2.三元运算 c = a+b if a>b else a-b if (a>b): a+b else: a-b 3.小知识( ...

  8. Java中的DateFormatter

    字母 日期或时间元素 表示 示例 G Era 标志符 Text AD y 年 Year 1996; 96 M 年中的月份 Month July; Jul;07 w 年中的周数 Number 27 W ...

  9. ubuntu 禁用快捷键

    System Settings > Keyboard > Shortcuts 点击要禁用的快捷键,按键盘"Backspace"键,就可以禁用了

  10. Docker 制作mysql镜像

    # 拉取Ubuntu镜像 docker pull docker.io/ubuntu: # 运行一个容器 docker run --name mysql -p 33:3306 -v /mysql:/va ...