本文是在学习中的总结,欢迎转载但请注明出处:http://blog.csdn.net/pistolove/article/details/48392713

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.

思路:

(1)题意为给定一个BST以及该树上两节点,求这两节点的最近公共祖先。

(2)该题比较简单,通过递归很好实现。如果两节点分别分布在根节点的左右子树上,那么它们的最近公共祖先只能是树根;如果两节点同时分部在根节点的相同子树上,则需要分别递归进行判定。

(3)详情见下方代码。希望本文对你有所帮助。

算法代码实现如下:

package leetcode;

import leetcode.utils.TreeNode;

/**
 *
 * @author liqqc
 *
 */
public class Lowest_Common_Ancestor_of_a_Binary_Search_Tree {

	public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
		// 在树的两边
		if (p.val >= root.val && q.val <= root.val)
			return root;

		else if (p.val <= root.val && q.val >= root.val)
			return root;

		// 在树的一边 左边或右边
		else if (p.val < root.val && q.val < root.val) {
			// 左边

			return lowestCommonAncestor(root.left, p, q);

		}

		else {

			// 右边

			return lowestCommonAncestor(root.right, p, q);

		}

	}
}

Leetcode_235_Lowest Common Ancestor of a Binary Search Tree的更多相关文章

  1. [geeksforgeeks] Lowest Common Ancestor in a Binary Search Tree.

    http://www.geeksforgeeks.org/lowest-common-ancestor-in-a-binary-search-tree/ Lowest Common Ancestor ...

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

  3. Lowest Common Ancestor of a Binary Search Tree、Lowest Common Ancestor of a Binary Search Tree

    1.Lowest Common Ancestor of a Binary Search Tree Total Accepted: 42225 Total Submissions: 111243 Dif ...

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

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

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

  7. LeetCode_Lowest Common Ancestor of a Binary Search Tree (Binary Tree)

    Lowest Common Ancestor of a Binary Search Tree 一.题目描写叙述 二.思路及代码 二叉搜索树有个性质:左子树的值都比根节点小,右子树的值比根节点大.那么我 ...

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

  9. 60-Lowest Common Ancestor of a Binary Search Tree

    Lowest Common Ancestor of a Binary Search Tree My Submissions QuestionEditorial Solution Total Accep ...

随机推荐

  1. Apache Beam—透视Google统一流式计算的野心

    Google是最早实践大数据的公司,目前大数据繁荣的生态很大一部分都要归功于Google最早的几篇论文,这几篇论文早就了以Hadoop为开端的整个开源大数据生态,但是很可惜的是Google内部的这些系 ...

  2. linux下连接windows的远程桌面

    拿ubuntu来举例: 1安装rdesktop 2 rdesktop -f 196.168.1.11:3389 3 哦鸟

  3. How to migrate data from another Mac using Mountain Lion and earlier

    链接:http://support.apple.com/zh-cn/HT4889

  4. 并发编程之ThreadLocal、Volatile、synchronized、Atomic关键字扫盲

    前言 对于ThreadLocal.Volatile.synchronized.Atomic这四个关键字,我想一提及到大家肯定都想到的是解决在多线程并发环境下资源的共享问题,但是要细说每一个的特点.区别 ...

  5. IBM SPSS 实习总结

    2015过完年,我知道导师要出国了,自己也算是水了一个idea 的论文.希望研二能找个实习,早听说西安IBM这边有学长在里面实习过,2月底联系了一下简历就塞了过去.面试就在锦业一路软件园他们上班的地方 ...

  6. jquery 只读

    大家都理解这是什么,正常的写法如下: if (status == true) { $("#minDelistStr").val(totalAmount);// 去掉首部的" ...

  7. Tom DeMarco:软件工程这个概念已过时?

    原文作者:Tom Demarco,写于2009年7月 作者简介:Tom DeMarco是大西洋系统协会(www.atlsysguild.com)的负责人.他的职业生涯开始于贝尔实验室,是结构化分析和设 ...

  8. FORM中的MOAC控制

    1.创建表时,对_ALL表创建同义词 -- Create Multi Org Synonym CREATE  OR REPLACE  SYNONYM CUX_WF_DEF_HEADER FOR CUX ...

  9. Cocos2D:塔防游戏制作之旅(八)

    如果所有东西通过检查,则创建一个新炮塔,将它放置在基座上,然后添加到towers数组中. 注意:在方法最后的bridge语法需要做一些解释.你下载的初始项目已经为一 些文件打开ARC,但不是Cocos ...

  10. Android光线传感器-android学习之旅(65)

    主要讲解光线传感器的使用,其实所有的传感器用法类似 主要是定义一个TextView用来显示光线强度,用完了以后记得在OnDestory里面释放资源 代码如下 public class MainActi ...