要求

  • 给定一棵二分搜索树和两个节点,寻找这两个节点的最近公共祖先

示例

  • 2和8的最近公共祖先是6
  • 2和4的最近公共祖先是2

思路

  • p q<node
  • node<p q
  • p<=node<=q

实现

 1 class Solution {
2 public:
3 TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) {
4
5 assert( p != NULL && q != NULL );
6
7 if( root == NULL )
8 return NULL;
9
10 if( p->val < root->val && q->val < root->val )
11 return lowestCommanAncestor( root->left , p , q );
12 if( p->val > root->val && q->val > root->val )
13 return lowestCommanAncestor( root->right , p , q );
14
15 return root;
16 }
17 };

相关

  • 98 Validate Binary Search Tree
  • 450 Delete Node in a BST
  • 108 Convert Sorted Array to Binary Search Tree
  • 230 Kth Smallest Element in a BST
  • 236 Lowest Common Ancestor of a Binary Tree

[刷题] 235 Lowest Common Ancestor of a Binary Search Tree的更多相关文章

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

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

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

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

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

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

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

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

  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. (easy)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. 【SqlServer】管理全文索引(FULL TEXT INDEX)

    Sql Server中的全文索引(下面统一使用FULLTEXT INDEX来表示全文索引),是一种特定语言搜索索引功能.它和LIKE的不一样,LIKE主要是根据搜索模板搜索数据,它的效率比FULLTE ...

  2. Nginx错误日志(error_log)配置及信息详解

    Nginx错误日志信息介绍 Nginx的错误信息是调试Nginx服务的重要手段,属于核心功能模块(ngx_core_module)的参数,该参数的名字为error_log,可以放在Main区块中全局配 ...

  3. java面试一日一题:java线程池

    问题:请讲下java中的线程池 分析:在面试中经常问到线程池的问题,要掌握其基本概念,使用方法,注意事项等,引申下tomcat中默认的线程数是多少 回答要点: 主要从以下几点去考虑, 1.为什么要使用 ...

  4. [Fundamental of Power Electronics]-PART I-4.开关实现-4.1 开关应用

    4.1 开关应用 4.1.1 单象限开关 理想的SPST(Single pole single throw)开关如图4.1所示.开关包含电源端子1和0,其电流和电压极性如图所示.在接通状态下,电压\( ...

  5. mysql 批量操作,已存在则修改,不存在则insert,同时判断空选择性写入字段

    注:如果是批量插入需要在 Java 连接数据库的字串中设置 &allowMultiQueries=true 针对单行数据有则修改无则新增 本案例的建表语句是: -- auto-generate ...

  6. java io系列

    java io系列01之 "目录" java io系列02之 ByteArrayInputStream的简介,源码分析和示例(包括InputStream) java io系列03之 ...

  7. (十六)Struts2的标签库

    一.简介 Struts2的标签库使用OGNL为基础,大大简化了数据的输出,也提供了大量标签来生成页面效果,功能非常强大. 在早期的web应用开发中,jsp页面主要使用jsp脚本来控制输出.jsp页面嵌 ...

  8. spring-cloud-gateway 服务网关

    Spring Cloud Gateway是Spring Cloud官方推出的第二代网关框架,取代Zuul网关.网关作为流量的,在微服务系统中有着非常作用,网关常见的功能有路由转发.权限校验.限流控制等 ...

  9. Sqlmap的基础用法(禁止用于非法用途,测试请自己搭建靶机)

    禁止用于非法用途,测试与学习请自己搭建靶机 sqlmap -r http.txt  #http.txt是我们抓取的http的请求包 sqlmap -r http.txt -p username  #指 ...

  10. [C++]变量声明与定义的规则

    声明与定义分离 Tips:变量能且仅能被定义一次,但是可以被多次声明. 为了支持分离式编译,C++将定义和声明区分开.其中声明规定了变量的类型和名字,定义除此功能外还会申请存储空间并可能为变量赋一个初 ...