【一天一道LeetCode】#235. Lowest Common Ancestor of a Binary Search Tree
一天一道LeetCode
本系列文章已全部上传至我的github,地址:ZeeCoder‘s Github
欢迎大家关注我的新浪微博,我的新浪微博
欢迎转载,转载请注明出处
(一)题目
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).”
![]()
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(p==NULL||q==NULL) return NULL;
stack<TreeNode*> st;
st.push(root);
TreeNode* max = p->val>q->val?p:q;//这里要找到给定两个节点中值较大的
TreeNode* min = p->val>q->val?q:p;//找到较小的
while(!st.empty()){
TreeNode* tmp = st.top();
st.pop();
if(tmp->val>=min->val&&tmp->val<=max->val) return tmp;//如果在范围内,代表找到了直接返回
else if(tmp->left!=NULL&&tmp->val<min->val) st.push(tmp->right);//如果在范围的右边,就继续往右子树找
else if(tmp->right!=NULL&&tmp->val>max->val) st.push(tmp->left);//如果在范围的左边,就继续往左子树找
else return NULL;//都不是就直接返回NULL
}
return NULL;
}
};
【一天一道LeetCode】#235. 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 ...
- 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 ...
- Java for LeetCode 235 Lowest Common Ancestor of a Binary Search Tree
递归实现如下: public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) { if(p.val>ro ...
- Leetcode 235 Lowest Common Ancestor of a Binary Search Tree 二叉树
给定一个二叉搜索树的两个节点,找出他们的最近公共祖先,如, _______6______ / \ ___2__ ___8__ / \ / \ 0 4 7 9 / \ 3 5 2和8的最近公共祖先是6, ...
- 【LeetCode】235. Lowest Common Ancestor of a Binary Search Tree (2 solutions)
Lowest Common Ancestor of a Binary Search Tree Given a binary search tree (BST), find the lowest com ...
随机推荐
- c++中成员函数的参数名与成员变量名重合的问题
有一天写类的时候突然想到了这个问题,下面就来介绍如何解决这个问题. 定义一个类: class test{ public: void setnum(); void getnum(); private: ...
- linux最常用的基本命令
//**********************对应linux centos常用命令 **************************/// 安装centos6.6带有gnome桌面 ctrl+c ...
- DCOM EXCE权限配置问题
检索COM 类工厂中 CLSID 为 {00024500-0000-0000-C000-000000000046}的组件时失败,原因是出现以下错误: 80070005: 关于以上这个问题,博主在百度上 ...
- SQL使用总结-like,MAX,MIN
1. 时间索引不容许使用like 对时间索引适应like,会时间索引变成字符串操作,成为遍历动作,失去索引价值. 错误写法: EXPLAIN SELECT AVG(data_value) AS av ...
- mysql insert插入时实现如果数据表中主键重复则更新,没有重复则插入的四种方法
[CSDN下载] Powerdesigner 设计主键code不能重复等问题 [CSDN博客] Oracle中用一个序列给两个表创建主键自增功能的后果 [CSDN博客] MySQL自增主键删除后重复问 ...
- 吴恩达深度学习第4课第3周编程作业 + PIL + Python3 + Anaconda环境 + Ubuntu + 导入PIL报错的解决
问题描述: 做吴恩达深度学习第4课第3周编程作业时导入PIL包报错. 我的环境: 已经安装了Tensorflow GPU 版本 Python3 Anaconda 解决办法: 安装pillow模块,而不 ...
- Python处理正则表达式超时的办法
最近在项目中遇到一个问题,就是需要采用正则匹配一些疑似暗链和挂马的HTML代码,而公司的老大给的正则表达式有的地方写的不够严谨,导致在匹配的时候发生卡死的现象,而后面的逻辑自然无法执行了.虽然用正则表 ...
- 使用translate将字符串中的数字提取出来
--方法1: with tmp as ( select '按时的撒旦123元(其中含存款11元)' name from dual union all select '一类似的预存9600元(新势力)' ...
- MySQL NOW() 函数
定义和用法 NOW() 返回当前的日期和时间. 语法 NOW() 实例 下面是 SELECT 语句: SELECT NOW(),CURDATE(),CURTIME() 结果如下所示: NOW() CU ...
- EntityFramework Core 自动绑定模型映射
笔者最近在和同事共同开发项目时,需要从他们提供的包含数据库实体类型的类库中读取实体信息绑定到自己的项目中(但是都在同一个解决方案里),所以很直接的一种方式就是把项目中所有的实体都以 public Db ...