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__
/ \ / \
_4
/ \

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.

思路:

  利用BST的性质,若待查找两个结点的数值都小于当前结点则向左边查找(若有一个等于当前结点则当前结点就是其最小的公共结点,结束查找),反之向右边查找(同理),若一个大于一个小于,则当前结点就是其最小的公共结点,结束查找。

C++:

 /**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
private:
TreeNode *l, *r, *ret;
public: void rec(TreeNode *root)
{
int v = root->val;
if(l->val <= v && r->val <= v)
{
if(v == l->val || v == r->val)
{
ret = root;
return ;
} if(root->left != )
rec(root->left);
}
else if(l->val < v && v < r->val)
{
ret = root;
return ;
}
else if(l->val >= v && r->val >= v)
{
if(v == l->val || v == r->val)
{
ret = root;
return ;
} if(root->right != )
rec(root->right);
}
} TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) {
if(root == )
return ; if(p->val < q->val){
l = p;
r = q;
}
else{
l = q;
r = p;
} ret = ; rec(root); return ret;
}
};

【LeetCode 235】Lowest Common Ancestor of a Binary Search Tree的更多相关文章

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

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

  3. 【LeetCode 236】Lowest Common Ancestor of a Binary Tree

    Given a binary tree, find the lowest common ancestor (LCA) of two given nodes in the tree. According ...

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

  5. 【LeetCode 235_二叉搜索树】Lowest Common Ancestor of a Binary Search Tree

    解法一:递归 TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) { if (root == NULL | ...

  6. LeetCode算法题-Lowest Common Ancestor of a Binary Search Tree

    这是悦乐书的第197次更新,第203篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第59题(顺位题号是235).给定二叉搜索树(BST),找到BST中两个给定节点的最低共 ...

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

  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

    235. Lowest Common Ancestor of a Binary Search Tree Easy Given a binary search tree (BST), find the ...

随机推荐

  1. BufferedReader方法-----Scanner方法

    import java.io.*; import java.util.Scanner; public class C { public static void main(String []args) ...

  2. 【web性能】页面呈现、重绘、回流

    在讨论页面重绘.回流之前.需要对页面的呈现流程有些了解,页面是怎么把html结合css等显示到浏览器上的,下面的流程图显示了浏览器对页面的呈现的处理流程.可能不同的浏览器略微会有些不同.但基本上都是类 ...

  3. 276. Paint Fence

    题目: There is a fence with n posts, each post can be painted with one of the k colors. You have to pa ...

  4. 怎么批量修改Word表格的宽度

    怎么批量修改Word表格的宽度 怎么批量修改Word表格的宽度呢.Word表格可根据窗口自动调整表格宽度,使得所有的表格宽度和页面宽度一样.当页面设置了新的页边距后,所有的表格都需要调整新的宽度.或者 ...

  5. powerdesigner jdbc 连接 oracle

    实验环境: powerdesigner 15 oracle 11g jdk1.6.0_43 提示:jdk必须选择32位,64位会报  "Could not Initialize JavaVM ...

  6. linux RTC 驱动模型分析【转】

    转自:http://blog.csdn.net/yaozhenguo2006/article/details/6824970 RTC(real time clock)实时时钟,主要作用是给Linux系 ...

  7. python中的 zip函数详解

    python中zip()函数用法举例 定义:zip([iterable, ...]) zip()是Python的一个内建函数,它接受一系列可迭代的对象作为参数,将对象中对应的元素打包成一个个tuple ...

  8. 【uva】1220 Party at Hali-Bula

    1. 题目描述公司里有$n, n \in [1, 200]$个人,他们间的关系构成树状结构.除老板外,每个员工都有唯一一个直属上司,要求从中选择尽量多的人,但是不能同时选择员工和他的直属上司,问最多能 ...

  9. 【Python】如何安装easy_install?

    [Python]如何安装easy_install? http://jingyan.baidu.com/article/b907e627e78fe146e7891c25.html easy_instal ...

  10. Phar文件

    phar 扩展名文件提供了一种将整个PHP应用程序打包放入一个被称之为phar(PHP archive)的文件从而更加容易便利地发布和安装的方法.就像是java的jar文件有点类似.除了这个功能外,P ...