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. IdentityServer4关于多客户端和API的最佳实践【含多类型客户端和API资源,以及客户端分组实践】【下】

    经过前两篇文章你已经知道了关于服务器搭建和客户端接入相关的基本资料,本文主要讲述整个授权系统所服务的对象,以ProtectApi资源为演示 目标: 1)实现多资源服务器针对请求的token校验,接入I ...

  2. url字符长度限制解决办法

    前段时间,同事往系统上传相关文档,发现输入失败,找到了我了. 开始以为数据库字段属性问题,修改后未解决随调试系统,发现没有走到后台程序,发现 ajax没有传值,各种测试问题情况,后来同事发现是url字 ...

  3. 转载 基于JAVA每月运势api调用代码实例

    代码描述:基于JAVA每月运势api调用代码实例 接口地址:http://www.juhe.cn/docs/api/id/58 原文链接:http://outofmemory.cn/code-snip ...

  4. vue按需引入element或mint

    vue按需引入element或mint需要添加 babel-preset-es2015 和babel-plugin-component

  5. Java 内部类的简单介绍

    内部类的三种分类(成员内部类,局部内部类,匿名内部类) 1.成员内部类 (类似于成员变量和成员方法) 在类的外部类的方法中去调用内部类  输出结果: 另一种直接在别的类中使用内部类,不过需要先创建外部 ...

  6. 安装 kubernetes v1.11.1

    kubernetes 版本 v1.11.1 系统版本:Centos 7.4 3.10.0-693.el7.x86_64 master: 192.168.0.205 node1: 192.168.0.2 ...

  7. 在Apex中使用sObject

    sObject对象的定义 Salesforce中的标准对象或自定义对象在Apex中使用时被称作"sObject".sObject对象的一个实例相当于Salesforce中的一条记录 ...

  8. Testlink Testlink在Windows下的安装

    Testlink在Windows下的安装   by:授客 QQ:1033553122   测试环境 testlink-1.9.14 下载地址:http://pan.baidu.com/s/1pLrcu ...

  9. uCrop图片裁剪

    uCrop使用 github地址 https://github.com/Yalantis/uCrop然后clone或下载到本地,运行之. 效果预览 app/build.gradle compile ' ...

  10. K邻近分类算法

    # -*- coding: utf-8 -*- """ Created on Thu Jun 28 17:16:19 2018 @author: zhen "& ...