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 ...
随机推荐
- 【hdu 6319】Ascending Rating
[链接] 我是链接,点我呀:) [题意] 给你一个长为n的数组a 让你对于每个长度为m的窗口. 算出其中的最大值以及从左往右遍历的时候这个最大值更新的次数. [题解] 单调队列. 从后往前滑动窗口. ...
- linux mint(Ubuntu、Debian) 18修改环境变量
修改环境变量 sudo gedit /etc/profile sudo gedit /etc/profile 在profile文件的末尾添加以下代码 export JAVA_HOME=/usr/lib ...
- spring boot约定优于配置的这种做法在如今越来越流行了
约定优于配置的这种做法在如今越来越流行了,它的特点是简单.快速.便捷.但是这是建立在程序员熟悉这些约定的前提上.而 Spring 拥有一个庞大的生态体系,刚开始转到 Spring Boot 完全舍弃 ...
- UNIX基础【UNIX入门经典】
最早在学校很流行.学生毕业以后就会为公司购买操作系统.导致UNIX流行 UNIX内核: Shell:sh csh ksh 其他组件:
- YAML说明
YAML说明 https://www.cnblogs.com/songchaoke/p/3376323.html XML的简化
- what's new in vc2015
1. 变量和函数的注解提示非常实用.象C#了. 2.CStdioFile升级了,不再须要象 vc2013中,用CStdioFileEx来修复错误了. 3. 发现再写.
- java结合jQuery.ajax实现左右菜单联动刷新列表内容
http://域名/一级菜单ID-二级菜单ID/ 用这种URL请求页面,出现如图所看到的内容: 该页面包括四部分,顶部文件夹+左側菜单+右側菜单+右下側数据列表. 左側菜单包括一级菜单和二级菜单,点击 ...
- 安卓 使用Gradle生成正式签名apk文件
1. 进入app中的build.gradle下面进行配置 2.进入Gradle下面选择clean和assembleRelese,双击 3.生成成功,前往查看 4.加密更安全
- JavaScript学习记录三
title: JavaScript学习记录三 toc: true date: 2018-09-14 23:51:22 --<JavaScript高级程序设计(第2版)>学习笔记 要多查阅M ...
- 计算label
func getCGSize(size:CGSize,fontSize:CGFloat,text:String)->CGSize{ let attributes = [NSFontAttribu ...