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.

解题思路:

节点a与节点b的公共祖先c一定满足:a与b分别出现在c的左右子树上(如果a或者b本身不是祖先的话)。

Java代码:

class TreeNode {
int val;
TreeNode left;
TreeNode right;
TreeNode(int x) { val = x; }
} public class Solution {
public static TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
if(root==null) return null;
if(root==p||root==q) return root;
TreeNode L=lowestCommonAncestor(root.left,p,q);
TreeNode R=lowestCommonAncestor(root.right,p,q);
if(L!=null&&R!=null) return root;
return L!=null?L:R;
}
//test
public static void main(String[] args) {
// TODO Auto-generated method stub
TreeNode tr=new TreeNode(1);
TreeNode nodeB=new TreeNode(2);
TreeNode nodeC=new TreeNode(3);
TreeNode nodeD=new TreeNode(4);
TreeNode nodeE=new TreeNode(5);
tr.left=nodeB;
tr.right=nodeC;
tr.left.right=nodeD;
tr.left.left=nodeE;
System.out.print(lowestCommonAncestor(tr, nodeC, nodeE).val);
} }

LeetCode (236):Lowest Common Ancestor of a Binary Search Tree的更多相关文章

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

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

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

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

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

  7. (easy)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 ...

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

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

随机推荐

  1. js/vue 高德地图绘制驾车路线图

    地图容器: // css要给此容器设置宽高 <div class="map_container"></div> 画图 data{ return { Clng ...

  2. java模块开发关键步骤

    1. 创建数据表 a) 确定表名(如:role) b) 确定表中的业务列(如:role_name.role_desc) c) 添加其它基本列 i. 如:role_id(主键).status(数据状态, ...

  3. mysql备份总结

    w汇总对比. mysqldump -u user -p wdbname > /www/wbak.sql pwd CREATE TABLE wbak_w_02071349 LIKE w; INSE ...

  4. C++之贪吃蛇

    #include<iostream> #include<cstdio> #include<cstdlib> #include<ctime> #inclu ...

  5. python [:-1] 与 [::-1]

    line = "abcde"line[:-1]结果为:'abcd' line = "abcde"line[::-1]结果为:'edcba' [:-1] b = ...

  6. Python的subprocess模块(二)

    原文:http://blog.chinaunix.net/uid-26000296-id-4461522.html 一.subprocess 模块简介 subprocess最早是在2.4版本中引入的. ...

  7. Flask用Flask-SQLAlchemy连接MySQL

    安装 pip3 install Flask-SQLAlchemy 测试环境目录结构 settings.py DIALECT = 'mysql' DRIVER = 'pymysql' USERNAME ...

  8. foo ?

    我们经常看到一些基础教程,面试题中经经常使用foo来命名,甚至有时候我们也会用过,可是你是否又知道foo是什么意思?(实际上,知道不知道又不会对你编码有不论什么影响~) 从编程黑马的王轶男的话来解释, ...

  9. U盘安装CentOS7笔记

    准备工具: 8G左右U盘; 最新版UltraISO; CentOS7光盘镜像; CentOS7的镜像文件可以在网易的开源镜像站或者阿里云的开源镜像站下载,地址分别是:http://mirrors.16 ...

  10. 0604-Zuul构建API Gateway-Zuul的回退

    一.概述 参看地址:https://cloud.spring.io/spring-cloud-static/Edgware.SR3/single/spring-cloud.html#hystrix-f ...