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 ...
随机推荐
- HDFS 安全模式的理解
安全模式是hadoop的一种保护机制,用于保证集群中的数据块的安全性. 当集群启动的时候,会首先进入安全模式.当系统处于安全模式时会检查数据块的完整性.假设我们设置的副本数(即参数dfs.replic ...
- spark基本概念及入门
spark spark背景 什么是spark Spark是一种快速.通用.可扩展的大数据分析引擎,2009年诞生于加州大学伯克利分校AMPLab,2010年开源,2013年6月成为Apache孵化项目 ...
- eclipse中安装windowbuilder插件、应用及卸载
一.安装windowbuilder插件 1.找到对应版本的windowbuilder 连接地址:http://www.eclipse.org/windowbuilder/download.php 如图 ...
- Java框架之Struts2(一)
在学习Struts2之前,我们要知道Java为什么要有反射机制呢?反射机制可以说是填补Java不能动态访问某一个类的空白.利用反射机制,动态的创建一个对象.动态的访问类的某个属性,而且访问哪一个属性自 ...
- Spring全家桶系列–SpringBoot之AOP详解
//本文作者:cuifuan //本文将收录到菜单栏:<Spring全家桶>专栏中 面向方面编程(AOP)通过提供另一种思考程序结构的方式来补充面向对象编程(OOP). OOP中模块化的关 ...
- Linux常用基本命令:三剑客命令之-awk格式化动作
我们之前说过,awk是一个超强的文本格式化工具,而本文的printf动作就是经常用来做格式化文本的.使用方式跟c语言的printf差不多. 1,printf默认不会回车换行 ghostwu@dev:~ ...
- AppBoxPro(权限管理框架--FineUIPro基础版+工厂模式+ADO.NET+存储过程)
FineUIPro基础版火爆来袭,特献上ADO.NET纯SQL方式AppBoxPro,希望大家能够喜欢! 下载源码请到[知识星球] https://t.zsxq.com/3rrNFyv
- api接口签名认证的一种方式
请求方 try { using (var client = new HttpClient()) { StringContent content = new StringContent(strParam ...
- .Net Core(二)EFCore
EFCore与之前的EF基本类似,区别在于配置的时候有一些差异:也取消了DB First和Model First,仅保留广泛使用的Code First模式:也不再支持LazyLoad.这里就感受一下 ...
- python第六十五天--python操作mysql
pymysql模块对mysql进行 import pymysql # 创建连接 conn = pymysql.connect(host='127.0.0.1', port=3306, user='ro ...