LeetCode 235 Lowest Common Ancestor of a Binary Search Tree(二叉搜索树的最小公共祖先)

简介: 版权声明:转载请联系本人,感谢配合!本站地址:http://blog.csdn.net/nomasp https://blog.csdn.net/NoMasp/article/details/50506260 翻译给定一个二叉搜索树(BST),找出两个给定节点的最小公共祖先(LCA)。
版权声明:转载请联系本人,感谢配合!本站地址:http://blog.csdn.net/nomasp https://blog.csdn.net/NoMasp/article/details/50506260

翻译

给定一个二叉搜索树(BST),找出两个给定节点的最小公共祖先(LCA)。

根据维基百科对于LCA的定义:“最小公共祖先的定义是对于两个节点v和s有一个最小的节点T,
以至于v和s都是T的后代(其中我们允许节点是自身的后代)。”
        _______6______
       /              \
    ___2__          ___8__
   /      \        /      \
   0      _4       7       9
         /  \
         3   5
例如,对于节点2和8的最小公共祖先是节点6.

另一个是2和4的LCA是2,因为根据LCA的定义,一个节点的是自身是后代。

原文

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.

分析

这个时候一定要先知道什么是BST,那么就来回顾一下:

这里写图片描述

图片来源于维基百科(有这么权威的资料,我就不自己瞎说了,逃……

博客发完之后发现图片看不清了,那就把文字节选下来好了,thanks,Wikipedia!

二叉查找树(英语:Binary Search Tree),也称二叉搜索树、有序二叉树(英语:ordered binary tree),排序二叉树(英语:sorted binary tree),
是指一棵空树或者具有下列性质的二叉树:
若任意节点的左子树不空,则左子树上所有结点的值均小于它的根结点的值;
任意节点的右子树不空,则右子树上所有结点的值均大于它的根结点的值;
任意节点的左、右子树也分别为二叉查找树;
没有键值相等的节点。

还是用擅长的递归吧,我好慌,每次都是效率不行的递归。

TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) {
    if (root == p || root == q) return root;
    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);
    }
    return  root;
}

根据LCA的定义,可以是节点自身,那么如果相等的话就可以直接返回啦。

如果任何一个为空了,结果不也为空么?

其余的两种情况,对应这个图来看看。对于3和5,如果(2)比它们都小,那就应该往右走了;对于0和2,如果(6)比它们都大,那就应该往左走了;对于3和7,如果(6)在它们中间,那就直接返回了。

        _______6______
       /              \
    ___2__          ___8__
   /      \        /      \
   0      _4       7       9
         /  \
         3   5

再高级的解法我还没想到,以后再补充~

代码

/**
* 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 {
public:
    TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) {
        if (root == p || root == q) return root;
        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);
        }
        return  root;
    }
};
目录
相关文章
|
5天前
leetcode代码记录(不同的二叉搜索树
leetcode代码记录(不同的二叉搜索树
7 0
|
29天前
Leetcode1038. 从二叉搜索树到更大和树(每日一题)
Leetcode1038. 从二叉搜索树到更大和树(每日一题)
|
2月前
leetcode热题100. 二叉树的最近公共祖先
leetcode热题100. 二叉树的最近公共祖先
22 0
|
3月前
|
Java
LeetCode题解-二叉搜索树中第K小的元素-Java
LeetCode题解-二叉搜索树中第K小的元素-Java
13 0
|
4月前
|
算法
代码随想录Day34 LeetCode T343整数拆分 T96 不同的二叉搜索树
代码随想录Day34 LeetCode T343整数拆分 T96 不同的二叉搜索树
31 0
|
4月前
|
存储 算法 测试技术
【深度优先】LeetCode1932:合并多棵二叉搜索树
【深度优先】LeetCode1932:合并多棵二叉搜索树
|
6天前
|
算法 C++
【刷题】Leetcode 1609.奇偶树
这道题是我目前做过最难的题,虽然没有一遍做出来,但是参考大佬的代码,慢慢啃的感觉的真的很好。刷题继续!!!!!!
8 0
|
6天前
|
算法 索引
【刷题】滑动窗口精通 — Leetcode 30. 串联所有单词的子串 | Leetcode 76. 最小覆盖子串
经过这两道题目的书写,相信大家一定深刻认识到了滑动窗口的使用方法!!! 下面请大家继续刷题吧!!!
10 0
|
6天前
|
算法
【刷题】 leetcode 面试题 08.05.递归乘法
递归算法是一种在计算机科学和数学中广泛应用的解决问题的方法,其基本思想是利用问题的自我相似性,即将一个大问题分解为一个或多个相同或相似的小问题来解决。递归算法的核心在于函数(或过程)能够直接或间接地调用自身来求解问题的不同部分,直到达到基本情况(也称为基础案例或终止条件),这时可以直接得出答案而不必再进行递归调用。
25 4
【刷题】 leetcode 面试题 08.05.递归乘法