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 ...
随机推荐
- repository和repertory
在研究.net core的时候知道了仓储这个概念,并发现两个单词repository和repertory 两者都有仓库,储藏所,储藏的意思,repository还指知识渊博的人,repertory除了 ...
- Mysql 存储过程实例详解
存储过程和函数是事先经过编译并存储在数据库中的一段SQL语句的集合,存储和和函数的区别在于函数必须有返回值,而存储过程没有,存储过程的参数可以使用IN.OUT.INOUT类型,而函数的参数只能是IN类 ...
- Flask 中的 特殊装饰器before_request/after_request
before_request :在请求收到之前绑定一个函数做一些事情. after_request: 每一个请求之后绑定一个函数,如果请求没有异常. teardown_request: 每一个请求之后 ...
- 微信小程序日历课表
最近项目中使用到了日历,在网上找了一些参考,自己改改,先看效果图 wxml <view class="date"> <image class="dire ...
- 【读书笔记】iOS-WiFi长连接
如果你的应用需要一个持久的WiFi长连接,你可以通过设置应用的Info.plist文件中的UIRequiresPersistentWiFi配置项的Boolean值来达到目的.如果这个配置项的值为YES ...
- Android开发常用的一些功能列表(转)
文章来源:http://www.cnblogs.com/netsql/archive/2013/03/02/2939828.html 1.软件自动更新下载,并提示 2.软件登录注册,以及状态保存 3. ...
- 喜闻乐见-Android应用的生命周期
本文主要讲述了App的启动流程.Application的生命周期以及进程的回收机制. 在绝大多数情况下,每一个Android应用都在自己的Linux进程中运行.当需要运行某些代码时,进程就会被创建.进 ...
- JavaScript大杂烩10 - 理解DOM
操作DOM 终于到了JavaScript最为核心的部分了,通常来说,操作DOM,为页面提供更为友好的行为是JavaScript根本目标. DOM树 - HTML结构的抽象 既然DOM是操纵HTML ...
- genymotion 模拟器内安装软件 the app contains ARM native code and your devices cannot run ARM instructions
问题如图: 解决方法: 下载一个Genymotion-ARM-Translation软件,安装到模拟器中就好了
- python第十八天
学习内容: json 模块,pickle模块,shelve模块,xml模块 json 模块 序列化: import json,pickle info={ 'name':'a', 'age':34, ...