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 ...
随机推荐
- Vue基础操作
一.Vue入门基础知识 1.Vue使用的基本操作 i. 先下载,引入vue.jsii. Vue,实例化一个vue实例化对象(new Vue({})) 1. 新建一个vue实例化对象(Vue是一个构造函 ...
- navicat连接SQL Sever数据库出现的问题
1.navicat 连接sqlserver提示要安装 sql server native client 解决办法:在navicat安装目录下有个sqlncli_x64.msi文件,双击安装一直下一步就 ...
- 【codeforces 812A】Sagheer and Crossroads
[题目链接]:http://codeforces.com/contest/812/problem/A [题意] 有一个小箭头指的那个地方; 指的就是人行道路; 然后p[i]指的就是那4个人行道是不是绿 ...
- table的创建
results为table的行信息 columnNames 是table列名 //创建并初始化table: table =new JTable(results,columNames); //设置ta ...
- MySQL 创建表时,设置时间字段自己主动插入当前时间
MySQL 创建表时,设置时间字段自己主动插入当前时间 DROP TABLE IF EXISTS `CONTENT`; CREATE TABLE `CONTENT` ( `ID` char(20) N ...
- Linux安装软件的几种方式
Linux下软件安装的方式主要有源码安装,rpm安装,yum安装,而常用的安装包主要有以下三种: tar包:例如software-1.2.3-1.tar.gz.它是使用UNIX系统的打包工具tar打包 ...
- iOS开发-Tools-CocoaPods
CocoaPods是什么? 当你开发iOS应用时,会经常使用到很多第三方开源类库,比如JSONKit,AFNetWorking等等.可能某个类库又用到其他类库,所以要使用它,必须得另外下载其他类库,而 ...
- Kettle学习系列之Kettle的起源
不多说,直接上干货! Kettle起源于十年以前,本世纪初.当时啊,ETL工具千姿百态,比较流行的工具有50个左右,ETL框架数量比工具还要多些. 根据这些工具的各自起源和功能可以分为以下4种类型,如 ...
- Storm Spout
本文主要介绍了Storm Spout,并以KafkaSpout为例,进行了说明. 概念 数据源(Spout)是拓扑中数据流的来源.一般 Spout 会从一个外部的数据源读取元组然后将他们发送到拓扑中. ...
- hiho1605 - 递推+1000000000=矩阵快速幂
题目链接 题目大意 小Hi最近对生成树(包含所有顶点的联通无环子图.)非常的感兴趣,他想知道对于特定的简单平面无向图是不是存在求生成树个数的简单方法. 小Hi定义了这样的图:一个以{0,1,2……n} ...