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.

/**
* 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)
return NULL;
TreeNode* tmp = root;
while (tmp)
{
if (tmp == p || tmp == q)
{//假设相等。则返回该结点
if (tmp == p)
return p;
else
return q;
}
else if ((p->val > tmp->val && q->val < tmp->val) ||
(p->val < tmp->val && q->val > tmp->val))
{//假设一个大于当前结点。一个小于当前结点。则返回当前结点
return tmp;
}
else if (p->val < tmp->val && q->val < tmp->val)
{//假设同一时候小于当前结点,则两个结点都在左子树其中
tmp = tmp->left;
}
else if (p->val > tmp->val && q->val > tmp->val)
{<span style="font-family: Arial, Helvetica, sans-serif;">//假设同一时候大于当前结点,则两个结点都在右子树其中</span>
				tmp = tmp->right;
}
}
return NULL;
}
};

leetCode(38):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 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 ...

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

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

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

随机推荐

  1. Crontab入门基础

    Crontab入门基础 crontab前言 crontab是Unix和Linux用于设置周期性被执行的指令,是互联网很常用的技术,很多任务都会设置在crontab循环执行,如果不使用crontab,那 ...

  2. idea 编辑器 光标问题!(insert键)

    今天写代码不小心按了键盘的insert键,光标莫名闪退了 ,重新打开的时候发现 光标变成了  按了insert 的效果  ,简直无语的要命啊! 这敲代码太恶心了!怒搜资料 结果找到了解决办法! 1.打 ...

  3. oracle 表空间Tablespaces

    1.表空间 一个数据库可以有多个表空间,一个表空间里可以有多个表.表空间就是存多个表的物理空间: 可以指定表空间的大小位置等. 创建表空间:create tablespace ts1 datafile ...

  4. ES学习——分析器和自定义分析器

    简介 es在对文档进行倒排索引的需要用分析器(Analyzer)对文档进行分析.建立索引.从文档中提取词元(Token)的算法称为分词器(Tokenizer),在分词前预处理的算法称为字符过滤器(Ch ...

  5. unity3d进程通信利用WM_COPYDATE和HOOK

    hello,近期用unity做了进程通信,应该是和c++的PC端实现通信,才開始一头雾水,后来实现了才知道好繁杂......先感谢对我提供帮助的百度,谷歌以及游戏圈的大大们. 在进程通信中非常多方法, ...

  6. Swift基础--定位

    // // ViewController.swift // JieCoreLocation // // Created by jiezhang on 14-10-4. // Copyright (c) ...

  7. Android实现天气预报温度/气温折线趋势图

     Android实现天气预报温度/气温折线趋势图 天气预报的APP应用中,难免会遇到绘制天气温度/气温,等关于数据趋势的折线或者曲线图,这类关于气温/温度的折线图,通常会有两条线.一条是高温线,一 ...

  8. 面向程序猿的设计模式 ——GoF《设计模式》读书总结(壹)抽象工厂&amp;生成器

    第一部分:创建型模式 创建型模式抽象了实例化过程. 它们帮助一个系统独立于怎样创建.组合和表示它的那些对象.(把一些小的对象组装成大对象,这个工作由专门的类对象来做) 一个类创建型模式使用继承改变被实 ...

  9. Cocos2d-x 多点触摸

    多点触摸的实现步骤与单点触摸类似,setTouchEnabled(true)开启触摸功能,注冊触摸事件,重载多点触摸函数:ccTouchesBegan(開始).ccTouchesMoved(移动).c ...

  10. HDU5233

    Gunner II Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total ...