要求

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

示例

  • 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. JavaSE(一)

    1.标识符 标识符是由数字,字母,下划线,$ 等进行命名的符号,但是不可以以数字开头: 标识符包含了关键字,变量名,他人定义,自己定义的. 2.关键字 关键字是指有特殊用途的符号.由以下50种构成 3 ...

  2. windows平台rust安装

    1.安装目录环境变量 RUSTUP_HOME D:\WorkSoftware\Rust\cargo CARGO_HOME D:\WorkSoftware\Rust\rustup 2.安装下载加速环境变 ...

  3. 最短路径(Dijskra算法)

    声明:图片及内容基于:https://www.bilibili.com/video/BV16C4y1H7Zc?from=articleDetail 最短路径 Dijkstra算法 原理 数据结构 核心 ...

  4. 给Nginx配置日志格式和调整日期格式

    效果对比 官方默认日志格式 # 官方默认日志格式 log_format main '$server_name $remote_addr - $remote_user [$time_local] &qu ...

  5. css盒模型以及如何计算盒子的宽度

    css盒模型以及如何计算盒子的宽度 盒模型 每个存在于可访问性树中的元素都会被浏览器绘制成一个盒子[1]. 每个盒子都可以看成由4部分组成,它们分别是 - 元素外边距(margin).元素边框(bor ...

  6. 原生对象写法,dom调用方法

    1 var App = (function () { 2     var App = function () { 3 //全局变量 4         this.init(); 5 this.a = ...

  7. BUAA_OO_第一单元

    BUAA_OO_2020_UNIT1 一.程序结构分析 第一次作业 UML & Mertrics ​ 由于数据处理简单,第一次作业中笔者发挥了面向过程的思想,将项转换成Biginteger, ...

  8. redis的持久化有哪几种方式?不同的持久化机制都有什么优缺点?(偏难)

    1.RDB和AOF两种持久化机制的介绍 RDB持久化机制,对redis中的数据执行周期性的持久化 AOF机制对每条写入命令作为日志,以append-only的模式写入一个日志文件中,在redis重启的 ...

  9. 自动化kolla-ansible部署ubuntu20.04+openstack-victoria之镜像上传-11

    自动化kolla-ansible部署ubuntu20.04+openstack-victoria之镜像上传-11 欢迎加QQ群:1026880196 进行交流学习 镜像上传 #controller1 ...

  10. kube-batch 创建的pod 一直是Pending

    官网的例子 apiVersion: batch/v1 kind: Job metadata: name: qj-1 spec: backoffLimit: 6 completions: 6 paral ...