【一天一道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 ...
随机推荐
- Visual Studio 2015 无法命中断点
新安装操作系统后发现,vs2015无法命中断点. 在项目中设置生成调试信息:FULL 即可.
- RabbitMQ-Spring AMQP
上篇文章RabbitMQ基础入门学习了rabbitMQ一些基础的api,当然spring也在原生代码的基础上做了更多的封装,这篇文章就基于spring-rabbit,学习一下spring的实现. p. ...
- day4 liaoxuefeng---函数
一.调用函数: 调用abs函数:取绝对值函数, >>> abs(100) 100 >>> abs(-20) 20 >>> abs(12.34) 1 ...
- 利用maven打jar包(项目来自GitHub)
1.首先去GitHub上clone一个项目.(前提,maven项目) 2.切换到项目根目录. 3.执行mvn package. 第一次操作失败: 分析错误原因(javac):maven无法找到jdk, ...
- 00-Unit_Common综述-RecyclerView封装
自学安卓也有一年的时间了,与代码相伴的日子里,苦乐共存.能坚持到现在确实已见到了"往日所未曾见证的风采".今2018年4月2日,决定用一个案例:Unit_Common,把安卓基础的 ...
- 设置思科设备console密码、enable密码、vty登录密码
思科设备各级密码:1) console密码 SW2(config)#line console 0SW2(config-line)#password ciscoSW2(config-line)#log ...
- 判断是否是IE9浏览器的最短语句 var ie=!-[1,]
没错,上面这个语句就可以判断浏览器是不是IE9以下的.why?1.[1,]在现代浏览器(ie包括ie9及以上)会被转换成[1], 而ie9以下就会转换成[1,undefined].2.分别对[1],和 ...
- FastDFS+Nginx安装配置
下载相关包: libevent-2.0.22-stable.tar.gz => https://github.com/libevent/libevent/releases/download/re ...
- Linux阿里云挂载磁盘,并开机自动挂载
Linux下磁盘挂载 公司新订购阿里云ECS,需要挂载当前的磁盘.暂时没有运维,自己动手挂载磁盘. 具体步骤如下: 1.查看是否已经分配 [root@iZ2ze1tefvghtbgkdur3xfZ / ...
- Linux文件基本操作
TIP:Tab键可以自动补全命令 首先要了解Linux树形结构 1./- 根每一个文件和目录从根目录开始.只有root用户具有该目录下的写权限.请注意,/root是root用户的主目录,这与/.不一样 ...