Lowest Common Ancestor of a Binary Search Tree (BST)

时间:2022-12-25 11:15:21

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

Node* LCA(Node* root, Node* p, Node* q)
{
if (!root || !p || !q)
return NULL;
if (max(p->data, q->data) < root->data)
return LCA(root->left, p, q);
else if (min(p->data, q->data) < root->data)
return LCA(root->right, p, q);
else
return root;
}

Lowest Common Ancestor of a Binary Search Tree (BST)的更多相关文章

  1. &lbrack;geeksforgeeks&rsqb; Lowest Common Ancestor in a Binary Search Tree&period;

    http://www.geeksforgeeks.org/lowest-common-ancestor-in-a-binary-search-tree/ Lowest Common Ancestor ...

  2. leetcode面试准备&colon;Lowest Common Ancestor of a Binary Search Tree &amp&semi; Binary Tree

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

  3. Lowest Common Ancestor of a Binary Search Tree、Lowest Common Ancestor of a Binary Search Tree

    1.Lowest Common Ancestor of a Binary Search Tree Total Accepted: 42225 Total Submissions: 111243 Dif ...

  4. leetcode 235&period; Lowest Common Ancestor of a Binary Search Tree 236&period; Lowest Common Ancestor of a Binary Tree

    https://www.cnblogs.com/grandyang/p/4641968.html http://www.cnblogs.com/grandyang/p/4640572.html 利用二 ...

  5. 【LeetCode】235&period; Lowest Common Ancestor of a Binary Search Tree &lpar;2 solutions&rpar;

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

  6. 235&period;236&period; Lowest Common Ancestor of a Binary &lpar;Search&rpar; Tree -- 最近公共祖先

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

  7. LeetCode&lowbar;235&period; Lowest Common Ancestor of a Binary Search Tree

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

  8. &lbrack;LeetCode&rsqb; 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. &lbrack;LeetCode&rsqb;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. Greedy&colon;Physics Experiment&lpar;弹性碰撞模型&rpar;&lpar;POJ 3848&rpar;

    物理实验 题目大意:有一个与地面垂直的管子,管口与地面相距H,管子里面有很多弹性球,从t=0时,第一个球从管口求开始下落,然后每1s就会又有球从球当前位置开始下落,球碰到地面原速返回,球与球之间相碰会 ...

  2. iOS开发之—— XCODE真机调试设备连接一直忙碌如何处理!(真机调试各种错误提示解决)

    真机调试,想连接真机调试代码可是连上设备后就一直转圈, 在Divice里面一直提示“iphone名称” is busy: Processing symbol files Xcode will cont ...

  3. Linux HugePages及MySQL 大页配置

    http://blog.csdn.net/dba_waterbin/article/details/9669929       ㈠ HugePages简介             HugePages是 ...

  4. JSP 实现 之 调用java方法实现MySQL数据库备份和恢复

    package cn.qm.db; import java.io.BufferedReader; import java.io.DataInputStream; import java.io.IOEx ...

  5. Programming In Scala笔记-第六章、函数式对象

    这一章主要是以定义和完善一个有理数类Rational为线索,分析和介绍有关类定义,构造函数,方法重写,变量定义和私有化,以及对操作符的定义等. 一.Rational类定义和构造函数 1.定义一个空类 ...

  6. input radio单选框绑定change事件

    html页面: <input type="radio" name="sex" value="female">female &lt ...

  7. 第四十九天 mysql 索引 元类

    一 昨日回顾 视图 触发器 事务 什么是事务 逻辑上的一组操作 要么都成功 要么都失败 如何使用 start transaction 开启事务 mysql 默认一条sql就是一个事务 pymysql默 ...

  8. Cache&colon; a place for concealment and safekeeping&period;Cache&colon; 一个隐藏并保存数据的场所

    原文标题:Cache: a place for concealment and safekeeping 原文地址:http://duartes.org/gustavo/blog/ [注:本人水平有限, ...

  9. View Stack容器,按钮选择子容器

    <?xml version="1.0" encoding="utf-8"?> <s:Application xmlns:fx="ht ...

  10. Qt中两种定时器用法

    在Qt中使用定时器有两种方法,一种是使用QObiect类的定时器:一种是使用QTimer类.定时器的精确性依赖于操作系统和硬件,大多数平台支持20ms的精确度. 1.QObject类的定时器 QObj ...