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
利用二叉搜索树的性质:左子树所有节点小于根节点,右子树所有节点大于根节点
如果两个节点的最大值小于根节点,那最低公共祖先一定在左子树,去左子树找;
如果两个节点的最小值大于根节点,那最低公共祖先一定在右子树,去右子树找;
其他情况下,当前节点一定就是最低公共祖先。其中,其他情况包括两种,即p是q或者q是p的祖先 和 p、q拥有一个共同祖先,就是当前这个节点,这个节点刚好将p、q划分到两个子树。
1.这个题目还包含一种情况就是,p是q或者q是p的祖先。所以在比较的时候使用的是>和<,如果出现了等于,就直接返回这个值了,不再递归调用。
2.最低公共祖先如果将两个节点分在不同的子树中,在二叉搜索树中,最低公共祖先的值一定介于两个节点值之间。
235. Lowest Common Ancestor of a Binary Search Tree
class Solution {
public:
TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) {
if(!root)
return NULL;
if(root->val > max(p->val,q->val))
return lowestCommonAncestor(root->left,p,q);
if(root->val < min(p->val,q->val))
return lowestCommonAncestor(root->right,p,q);
else
return root;
}
};
依旧有两种情况:一个是p是q或者q是p的祖先,另一个是两个共用一个祖先
迭代返回的值只可能是NULL、p、q三种情况
如果left、right分别返回了p、q,那证明当前节点一定是最低公共祖先(因为是递归的,所以才是最低的,真正的返回值是在那个第一次获得left、right的节点返回的值)
这个题是直接搜索,就是判断节点是否等于我们需要寻找的两个节点
236. Lowest Common Ancestor of a Binary Tree
class Solution {
public:
TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) {
if(!root || root == p || root == q)
return root;
TreeNode* left = lowestCommonAncestor(root->left,p,q);
TreeNode* right = lowestCommonAncestor(root->right,p,q);
if(left && right)
return root;
return left ? left : right;
}
};
leetcode 235. Lowest Common Ancestor of a Binary Search Tree 236. Lowest Common Ancestor of a Binary Tree的更多相关文章
- [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 ...
- Foundation: Binary Search
/* Binary search. * * Implementation history: * 2013-10-5, Mars Fu, first version. */ /* [Binary Sea ...
- 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
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...
- [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 解题报告(Java & Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 [LeetCode] https://leet ...
- 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 ...
随机推荐
- [android] 手机卫士手机实现短信指令获取位置
获取位置 新建一个service的包 新建一个GPSService类继承系统的Service类 清单文件中注册一下 重写onCreate()方法,服务创建的时候回调 重写onDestroy()方法, ...
- CSS学习笔记09 简单理解BFC
引子 在讲BFC之前,先来看看一个例子 <!DOCTYPE html> <html lang="en"> <head> <meta cha ...
- Matlab illustrate stiffness
% matlab script to illustrate stiffness % using simple flame propagation model close all clear all % ...
- 关于IOS下click事件委托失效的解决方案
一.由于某些特殊情况下,需要用到事件委托,比如给动态创建的DOM绑定click事件,这里就需要事件委托(这里就牵扯到:目标元素和代理元素)目标元素:动态创建的元素,最终click事件需要绑定到该元素 ...
- php获取指定月份月初和月末的时间戳
获取指定月份的开始时间戳和结束时间戳,只需传入年月即可(2018-01,2018-1两种格式都可以) $data['sel_time'] = '2018-11'; $data['begin_time' ...
- html 字符串 生成 pdf 完美解决中文不显示
//maven <dependency> <groupId>com.itextpdf</groupId> <artifactId>itextpdf< ...
- ionic3 细节注意
一.图标和splash大小不一样 icon图标的大小尽量为1024*1024,并且不能为圆角. splash图片的大小尽量为2732*2732,ionic1的大小为2208*2208
- Element隐藏组件:scrollbar
scrollbar是用来替代浏览器原生滚动条的组件,element的文档中并没有对scrollbar的描述. 使用方法:以<el-scrollbar/>包裹要滚动的元素,并设置固定高度.在 ...
- 根据学习廖雪峰老师的git教程做的笔记
根据学习廖雪峰老师的git教程做的笔记 安装git 进行git的配置 配置您的用户名和邮箱地址,使用--global 这个参数表明了在此台机器上的所有仓库都会使用该配置 $ git config -- ...
- python第二十天
logging模块 re正则表达式