本文是在学习中的总结,欢迎转载但请注明出处: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. mysql进阶(二十六)MySQL 索引类型(初学者必看)

    mysql进阶(二十六)MySQL 索引类型(初学者必看)   索引是快速搜索的关键.MySQL 索引的建立对于 MySQL 的高效运行是很重要的.下面介绍几种常见的 MySQL 索引类型.   在数 ...

  2. 保存图片到SD卡

    添加SD卡写权限 方法 public void saveMyBitmap(String bitName, Bitmap mBitmap) { File f = new File("/sdca ...

  3. 学习TensorFlow,打印输出tensor的值

    在学习TensorFlow的过程中,我们需要知道某个tensor的值是什么,这个很重要,尤其是在debug的时候.也许你会说,这个很容易啊,直接print就可以了.其实不然,print只能打印输出sh ...

  4. Github客户端以及Git shell的使用

    昨天介绍了怎么使用Git Shell来commit我们的代码,但是这都是简单的操作,我们还没有使用到Github是怎么进行版本控制的呢.所以,今天就来介绍一下,怎么来做版本控制吧. 必备材料 首先要确 ...

  5. 皮尔森相似度计算举例(R语言)

    整理了一下最近对协同过滤推荐算法中的皮尔森相似度计算,顺带学习了下R语言的简单使用,也复习了概率统计知识. 一.概率论和统计学概念复习 1)期望值(Expected Value) 因为这里每个数都是等 ...

  6. Android初级教程:单击事件的传递机制初谈

    以上仅是小试牛刀,后续有很多事件传递机制,继续探讨.

  7. Qzone React Native改造

    Android Qzone 6.1版本在情侣空间涉水React Native,以动态插件方式将情侣空间进行React Natived的改造.在情侣空间基础上,Android Qzone 6.2版本以融 ...

  8. (一一五)利用NSKeyedArchiver实现任意对象转为二进制

    [应用背景] 在数据库中存储数据时,如果对象过于复杂,又不必要创建复杂的表,可以直接把整个对象转化为二进制存入数据库字段,然后取出后再还原即可. [实现方法] 在PHP中,使用序列化和反序列化可以实现 ...

  9. 精通CSS+DIV网页样式与布局--初探CSS

    CSS英文名Cascading Style Sheet,中文名字叫层叠样式表,是用于控制页面样式并允许将样式信息与网页内容分离的一种标记性语言,DIV+CSS是WEB设计标准,它是一种网页的布局方法. ...

  10. 【Unity技巧】制作一个简单的NPC

    1. 写在前面 前几天看了cgcookie的一个教程,学习了下怎么根据已有人物模型制作一个仿版的NPC人物,感觉挺好玩的,整理一下放到博客里! 先看一下教程里面的最终效果. 是不是很像个幽灵~ 下面是 ...