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.

分析:由于在所谓的二叉搜索树(binary search tree)中,处处满足顺序性(即任一节点的左(右)子树种,所有节点均小于(大于)r)。

思路: 1、当头结点为空时,返回空指针

2、如果节点p、q的值都比root的值要小,那么LCA一定为root的左子树;而如果节点p、q的值都比root的值要大,那么LCA一定为root的右子树;当节点p、q的值中一个比root的值要大,另一个比它小时,LCA就是root了。

3、我们要考虑是否能覆盖到节点是它自身子节点的情况,这时返回的是p或q的其中一个。

代码如下:(recursive solution)

/**
* 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) {
while(root !=nullptr){
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);
}
else return root; // 当p->val = root->val或 q->val =root->val时,就是节点是它自身子节点的情况了
}
return root;
}
};

 也可以简洁点:

class Solution {
public:
TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) {
if (p -> val < root -> val && q -> val < root -> val)
return lowestCommonAncestor(root -> left, p, q);
if (p -> val > root -> val && q -> val > root -> val)
return lowestCommonAncestor(root -> right, p, q);
return root;
}
};

  

其他参考解法:

class Solution {
public:
TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) {
if(p->val > root->val && q->val < root->val)
{
return root;
}
if(p->val < root->val && q->val > root->val)
{
return root;
}
if( p->val == root->val || q->val == root->val)
return root; if( p->val < root->val && q->val < root->val)
return lowestCommonAncestor(root->left, p, q);
if( p->val > root->val && q->val > root->val)
return lowestCommonAncestor(root->right, p, q);
}
};

或:(iterative solution)  

class Solution {
public:
TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) {
TreeNode* cur = root;
while (true) {
if (p -> val < cur -> val && q -> val < cur -> val)
cur = cur -> left;
else if (p -> val > cur -> val && q -> val > cur -> val)
cur = cur -> right;
else return cur;
}
}
};

  

 

leetcode: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. [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 ...

  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 (二叉搜索树最近的共同祖先)

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

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

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

  8. LeetCode (236):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. 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 ...

随机推荐

  1. poi生成excel

    转自:http://www.cnblogs.com/bmbm/archive/2011/12/08/2342261.html 1.首先下载poi-3.6-20091214.jar,下载地址如下: ht ...

  2. PE文件结构学习

    PE:Portable Executable File Format(可移植的执行体).Windows平台主流可执行文件格式..exe与.dll文件都是PE格式.32位的叫做PE32,64位的叫做PE ...

  3. Goolg Chrome 插件开发--Hello world

    开发Chrome插件很简单,只要会web开发(html+javascript+css)那么这个就是能驾轻就熟,只需要了解一下插件具体的运行环境及要求就OK了. 1.先创建一个html文件,名字随便取, ...

  4. Assetbundle的杂七杂八

    使用Assetbundle时可能遇到的坑 一 24 十一郎未分类 No Comments 转自 http://www.unitymanual.com/blog-3571-132.html 1.Edit ...

  5. c#实现Socket网络编程

    命名空间: 在网络环境下,我们最感兴趣的两个命名空间是System.Net和 System.Net.Sockets. System.Net命名空间通常与较高程的操作有关,例如download或uplo ...

  6. KMP模板,最小循环节

    (可以转载,但请注明出处!) 下面是有关学习KMP的参考网站 http://blog.csdn.net/yaochunnian/article/details/7059486 http://blog. ...

  7. kafka配置

    官网:http://kafka.apache.org/ 主要有3种安装方式: 1. 单机单broker 2. 单机多broker 3. 多机多broker 1. wget http://mirror. ...

  8. Good Bye 2015 B. New Year and Old Property 计数问题

    B. New Year and Old Property   The year 2015 is almost over. Limak is a little polar bear. He has re ...

  9. hdu 2413(最大匹配+二分)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2413 思路:由于要求最少的时间,可以考虑二分,然后就是满足在limit时间下,如果地球战舰数目比外星战 ...

  10. web工程导入MyEclipse 就变成Java工程 ———— 解决方案

    Web 工程 导入到 MyEclipse 中后就变成 Java工程了 折腾大大半天,最后才发现是 .settings 里面文件的配置问题.. .settings 文件夹里面的 org.eclipse. ...