题目:

Given a binary search tree (BST), find the lowest common ancestor (LCA) of two given nodes in the BST.

According to the definition of LCA on Wikipedia: “The lowest common ancestor is defined between two nodes v and w as the lowest node in T that has both v and w as descendants (where we allow a node to be a descendant of itself).”

        _______6______
/ \
___2__ ___8__
/ \ / \
0 _4 7 9
/ \
3 5

For example, the lowest common ancestor (LCA) of nodes 2 and 8 is 6. Another example is LCA of nodes 2 and 4 is 2, since a node can be a descendant of itself according to the LCA definition.

提示:

此题的核心是要利用已知条件中,给定的二叉树是二叉搜索树这一条件。所谓二叉搜索树,指的是一种二叉树,其中,比根节点小的节点都位于根节点的左侧,反之,若比根节点要大,则位于根节点的右侧。可以参考题目中的例子。

递归调用的思路也很简单:

  • 若两个要搜索的节点都比根节点小,则对根节点的左节点进行递归;
  • 若两个要搜索的节点都比根节点打,则对根节点的右节点进行递归;
  • 若一大一小,则返回根节点。

代码:

/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) {
if (p->val < root->val && q->val < root->val)
return lowestCommonAncestor(root->left, p, q);
if (p->val > root->val && q->val > root->val)
return lowestCommonAncestor(root->right, p, q);
return root;
}
};

由于此题的测试用例中没有节点为NULL的情况,因此代码中省去了空指针的判断。

【LeetCode】235. Lowest Common Ancestor of a Binary Search Tree的更多相关文章

  1. 【LeetCode】235. Lowest Common Ancestor of a Binary Search Tree (2 solutions)

    Lowest Common Ancestor of a Binary Search Tree Given a binary search tree (BST), find the lowest com ...

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

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

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

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

  4. 【easy】235. Lowest Common Ancestor of a Binary Search Tree

    题意大概是,找两个节点的最低公共祖先 /** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNod ...

  5. LeetCode OJ 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 ...

  6. 【leetcode❤python】235. Lowest Common Ancestor of a Binary Search Tree

    #-*- coding: UTF-8 -*- # Definition for a binary tree node.# class TreeNode(object):#     def __init ...

  7. 【LeetCode】236. Lowest Common Ancestor of a Binary Tree 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

  8. 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 利用二 ...

  9. leetcode面试准备:Lowest Common Ancestor of a Binary Search Tree & Binary Tree

    leetcode面试准备:Lowest Common Ancestor of a Binary Search Tree & Binary Tree 1 题目 Binary Search Tre ...

随机推荐

  1. Windows安装Mysql5.7.10绿色版

    今天在Windows上安装Mysql的时候,去官网上下了一个最新版本的Mysql5.7.10绿色版,但是之前网上安装方式都过时了,比如会报一些常见的错误“[ERROR] Fatal error: Ca ...

  2. Java学习笔记——设计模式之二.策略模式

    明确是王道 --Clean Code 先定义策略类 package cn.no2.strategy; public abstract class Strategy { //省略属性 //算法方法 pu ...

  3. Charles安装破解及使用

    摘要 在发开过程中,追踪请求和监控请求与返回数据是我们经常会需要的一个需求,在Mac端,Charles是一款非常易用的抓包工具. Mac端的优秀抓包工具--Charles使用 一.简介 Charles ...

  4. 【wannacry病毒之暗网】-如何访问"暗网"(慎入)

    心里能力不强的人,请别看. 有些事情还是不要接触比较好, 社会最恶一面不是随随便便就能接触到的, 也不是你能理解的 你想要用暗网做什么是你考虑的一个问题 什么是暗网? 所谓的"暗网" ...

  5. 【转】c++ new操作符的重载

    基本概念: 1. 操作符重载:C++支持对某个操作符赋予不同的语义 2. new操作符:申请内存,调用构造函数 关于c++ new操作符的重载 你知道c++ 的new 操作符和operator new ...

  6. 关于listener监听器的一些记录

    实现ServletContextListener后,需要实现2个方法,一个是contextInitialized,这个方法会在context被创建的时候执行,这个方法有一个参数为ServletCont ...

  7. EMC在线测试题目及答案 绿色为正确答案,红色为错误答案

    1. 以下哪一项技术可以将IT的物理资源放在一个共享池中以及提升它们的利用率? 分区 虚拟化 协调 LUN 屏蔽 2. 哪一项是EMC的基于块-存储(block-based)的高端存储? Atmos ...

  8. 将C-风格字符串用作string对象引用参数

    string类定义了一种char*到string的转换功能,这使得可以使用C-风格字符串来初始化string对象. 类型为const引用的形参其中一个属性表明:假设实参的参数类型与引用参数不匹配,但可 ...

  9. 备忘录《一》基于cookie使用拦截器实现客户每次访问自登陆一次

    原创声明:本文为本人原创作品,绝非他处摘取,转载请联系博主 相信大家在各大网站都会遇到,登录时,在登录框出现下次免登陆/一个月免登陆的类似选项,本次博文就是讲解如何实现,在这记录一下,也算是做个备忘录 ...

  10. Ajax01 什么是ajax、获取ajax对象、ajax对象的属性和方法

    1 什么是ajax ajax是一种用来改善用户体验的技术,其本质是利用浏览器提供的一个对象(XMLHttpRequest,也可称之为ajax对象) 向服务器发送异步请求;服务器返回部分数据(不是一个完 ...