leetCode(38):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 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的更多相关文章
- 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 利用二 ... 
- [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 ... 
- [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 ... 
- 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 ... 
- 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 ... 
- (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 ... 
- 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 ... 
- 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 ... 
- 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 ... 
随机推荐
- [luogu] P3210 [HNOI2010]取石头游戏(贪心)
			P3210 [HNOI2010]取石头游戏 题目描述 A 公司正在举办一个智力双人游戏比赛----取石子游戏,游戏的获胜者将会获得 A 公司提供的丰厚奖金,因此吸引了来自全国各地的许多聪明的选手前来参 ... 
- iOS 图像处理-调整图像亮度
			- (UIImage*) getBrighterImage:(UIImage *)originalImage { UIImage *brighterImage; CIContext *context ... 
- android全磁盘加密
			android 全磁盘加密 什么是全磁盘加密? 全磁盘加密是使用一个密钥来为android设备上全部的用户数据加密的过程.一旦设备被加密,全部的用户创建的数据都将会在提交的磁盘之前自己主动加密,在读取 ... 
- [BLE--Link Layer]设备蓝牙地址
			简述 不论什么网络设备而言,都会有自己独特的一个MAC地址,不然在设备量较大的情况下非常可能造成通信的混乱.蓝牙是无线通信中使用非常广泛的技术.当然其蓝牙地址也是相当的重要的了. 蓝牙地址简述 种类划 ... 
- windowsclient崩溃分析和调试
			本文介绍windows上崩溃分析的一些手段,顺便提多进程调试.死锁等. 1.崩溃分析过程 1.1 确认错误码 不管是用windbg还是用vs.首先应该注意的是错误码,而90%以上的崩溃都是非法訪问. ... 
- Java7里try-with-resources分析--转
			原文地址:http://blog.csdn.net/hengyunabc/article/details/18459463 这个所谓的try-with-resources,是个语法糖.实际上就是自动调 ... 
- CSS弹性盒模型flex概念
			盒模型分为:标准w3c盒模型.IE盒模型.以及css中的伸缩盒模型. 先说CSS的伸缩盒模型:flex模型是CSS3引入的新的布局模型,是flexible box的缩写,一般称之为弹性盒模型.和CSS ... 
- C#调用webservice(二)
			第二篇调用webservice,web服务是http://webservice.webxml.com.cn/webservices/DomesticAirline.asmx,航班查询服务 添加web服 ... 
- 推箱子小游戏《格鲁的实验室》13关 - bfs最短路径
			下载了一款推箱子小游戏,第13关的时候怎么也破不了最佳纪录(最少步数是9而我们最好的方案是10步),因为数据比较小(6*8的方阵),所以写了个BFS来找最短路. 游戏的目标是把小黄人推到黄色球,小绿人 ... 
- HDU 1548 A strange lift【BFS】
			题意:给出一个电梯,给出它的层数f,给出起点s,终点g,以及在每一层能够上或者下w[i]层,问至少需要按多少次按钮到达终点. 和POJ catch that cow一样,直接用了那一题的代码,发现一直 ... 
