要求

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

示例

  • 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. MarkDown-简单学习

    标题 注意:首部添加1-6个"#"号来设置标题大小: 字体 1:粗体 (注意:首尾同时添加2个"*"号来设置) 2:斜体 (注意:首尾同时添加1个"* ...

  2. Elasticsearch 基础介绍

    # Elasticsearch简介 ## 基础概念 ​ Elasticsearch由Shay banon在2004年进行初步开发,并且在2010年2月发布第一个版本. ​ 此后Shay banon在2 ...

  3. (原创)高DPI适配经验系列:(一)缩放比例与DPI对应关系

    一.前言 当下,2K分辨率已成为主流标配,3K.4K也已经广泛应用. 在屏幕尺寸不变的情况下,高分辨率也就意味着高DPI,对于桌面程序而言,除了先天就支持高DPI的框架外(如UWP.Electron等 ...

  4. B. 【例题2】移位包含

    解析 判断是否是子串,可以将这个一个环 #include <bits/stdc++.h> using namespace std; int f = 0; string a, b; int ...

  5. 开源一周岁,MindSpore新特性巨量来袭

    摘要:MindSpore很多新特性与大家见面了,无论是在效率提升.易用性,还是创新方面,都是干货满满. 最近,AI计算框架是业界的热点,各大厂商纷纷投身AI框架的自研发,究其原因:AI框架在整个人工智 ...

  6. Docker工具的使用

    初识 Docker jdk的版本问题,环境造成的问题很常见,称为代码的水土不服 把环境和代码一起传过去 软件跨环境迁移的问题就解决了 Docker 是一个开源的应用容器引擎 诞生于 2013 年初,基 ...

  7. 「Spring Boot 2.4 新特性」启动耗时详细监控

    背景 Spring Boot 项目随着项目开发过程中引入中间件数量的增加,启动耗时 逐渐增加. 笔者在 <Spring Boot 2.4.0 正式 GA,全面拥抱云原生>文章评论下发现了 ...

  8. day-8 xctf-guess_num

    xctf-guess_num 题目传送门:https://adworld.xctf.org.cn/task/answer?type=pwn&number=2&grade=0&i ...

  9. 自动化kolla-ansible部署ubuntu20.04+openstack-victoria单机

    自动化kolla-ansible部署ubuntu20.04+openstack-victoria单机 欢迎加QQ群:1026880196 进行交流学习 一. 环境信息 1. 硬件信息 型号:Dell ...

  10. JAVAEE_Servlet_06_ServletContext接口

    ServletContext接口 * javax.servlet.ServletContext * Tomcat服务器中ServletContecxt的完整类名: ServletContext:org ...