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. [geeksforgeeks] Lowest Common Ancestor in a Binary Search Tree.

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

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

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

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

  6. 235.236. Lowest Common Ancestor of a Binary (Search) Tree -- 最近公共祖先

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

  7. LeetCode_235. 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. [LeetCode] 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. [LeetCode]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. QWebView 显示本地HTML 文件

    QWebView 显示本地HTML文件的时候,如果直接使用 webView->load(QUrl(QString("file:///c:\\a.html")); 可能会导致a ...

  2. Crossing River poj1700贪心

    题目描述:N个人过河,只有一只船,最多只能有两人划船,每个人划船速度不同,船速为最慢的人的速度.输入T为case个数,每个case输入N为人数,接下来一行输入的是每个人过河的时间,都不相同.要求输出N ...

  3. python-大话装饰器

    装饰器 装饰器是个什么玩意呢?是个会了不难,装逼神器.但是如果不了解理解起来还是很抽象,让我们试试这个装逼神器吧! 1.什么是装饰器 装饰器是一个很著名的设计模式,经常被用于有切面需求的场景,较为经典 ...

  4. iOS定位功能

    1.实现定位功能需要导入系统库MapKit.framework 2.在iPhone手机上默认是禁止手机定位的,所以,要询问系统是否开启手机定位功能. 为了开启手机定位功能,还需在info.plist中 ...

  5. ASP.NET MVC4 ASP.NET Web API路由规则

    using System;using System.Collections.Generic;using System.Linq;using System.Net;using System.Web.Ht ...

  6. 662 - Fast Food

    描述:状态方程p[i][j]=dp[i-1][k]+dist(k+1,j),由于没搞懂距离dist是怎么计算的,以为是num[j]-num[k+1],结果wa了一次,在状态转移的时候,采用一个数组sc ...

  7. mvc下载文件

    MVC下载文件方式 方式一: public FileStreamResult DownFile(string filePath, string fileName)  {       string ab ...

  8. log4j 详解

      转载自:http://www.blogjava.net/hwpok/archive/2008/08/23/223891.html >>>>1. 概述<<< ...

  9. 关于Scrapy框架的基本概念

    Scrapy爬取网页基本概念 Scrapy爬取网页基本概念 怎么样用Scrapy生成project? scrapy startproject xxx 如何用Scrapy爬取网页? import scr ...

  10. Android 汉字转拼音之JNI篇

    package com.tool.hz2py; import android.os.Bundle; import android.app.Activity; import android.view.M ...