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的更多相关文章

  1. [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 ...

  2. [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 ...

  3. Foundation: Binary Search

    /* Binary search. * * Implementation history: * 2013-10-5, Mars Fu, first version. */ /* [Binary Sea ...

  4. 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 ...

  5. 【一天一道LeetCode】#235. Lowest Common Ancestor of a Binary Search Tree

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...

  6. [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 ...

  7. 【LeetCode】235. Lowest Common Ancestor of a Binary Search Tree 解题报告(Java & Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 [LeetCode] https://leet ...

  8. 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 ...

  9. 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 ...

随机推荐

  1. sort、sorted高级排序-Python3.7 And 算法<七>

    1.sort(*, key=None, reverse=False) sort()接受两个参数,这两个参数只能通过关键字(关键字参数)传递. 参数key:带一个参数的函数(排序时,会依次传入列表的每一 ...

  2. Hive原理总结(完整版)

    目录 课程大纲(HIVE增强) 3 1. Hive基本概念 4 1.1 Hive简介 4 1.1.1 什么是Hive 4 1.1.2 为什么使用Hive 4 1.1.3 Hive的特点 4 1.2 H ...

  3. IDEA乱码解决

    设置中encoding都指定为utf-8,完全没用. 最终在java编译的地方找到了解决办法

  4. EL表达式和JSTL的使用

    一:EL表达式 1.概述:在jsp开发中,为了获取Servlet域对象中存储的数据,经常要写很多java代码,这样的做法会使JSP页面混乱,难以维护,为此,在JSP2.0规范中提供了EL表达式.它是E ...

  5. 随机生成n个不重复的数,范围是2-32,并让其在新页面打开

    var n = 5 var timer; function suiji(){ var arr = [] // 循环生成n个随机数 for(var i=0;i<n;i++){ var num = ...

  6. Csharp:Paging Sorting Searching In ASP.NET MVC 5

    http://www.c-sharpcorner.com/UploadFile/0c1bb2/sorting-paging-searching-in-Asp-Net-mvc-5/ https://dz ...

  7. python之编码和解码

    编码: 1. ascii. 有: 数字, 字母, 特殊字符. 8bit 1byte 128 最前面是0 2. gbk. 包含: ascii, 中文(主要), 日文, 韩文, 繁体文字. 16bit, ...

  8. loadrunner 脚本开发-定义全局变量

    脚本开发-定义全局变量 by:授客 QQ:1033553122 如果参数是全局的,在脚本中的任何一个Action中都可以使用,变量一般是局部的,如果跨Action调用会出现未声明的错误. 打开Scri ...

  9. java代码代替xml实现图片

    1.使用StateListDrawable替换selector public static StateListDrawable getSelector(Drawable normalDrawable, ...

  10. QQ浏览器兼容模式问题

    今天客户反馈有个问题,他说用360浏览器的兼容模式无法登陆系统,我试了可以,接着试了IE11,也可以,然后跟经理汇报,他说他用qq浏览器兼容模式就不可以,于是我试了,果然不可以... 问题是酱紫的:输 ...