Lowest Common Ancestor of a Binary Search Tree (BST)
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)的更多相关文章
- [geeksforgeeks] Lowest Common Ancestor in a Binary Search Tree.
http://www.geeksforgeeks.org/lowest-common-ancestor-in-a-binary-search-tree/ Lowest Common Ancestor ...
- 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 ...
- 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 ...
- 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 利用二 ...
- 【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 ...
- 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 ...
- 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 ...
- [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 ...
- [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 ...
随机推荐
- uva 10129 poj 1386 hdu 1116 zoj 2016 play on words
//本来是想练一下欧拉回路的,结果紫书上那题是大水题!!!!! 题意:给出n个单词,是否可以把单词排列成每个单词的第一个字母和上一个单词的最后一个字母相同 解:欧拉通路存在=底图联通+初度!=入度的点 ...
- opennebula kvm attach disk
openNebula hotPlug disk or nic 网络检索关键字(Network search keywords) 208.117.233.122 virsh attach disk vi ...
- #include <algorithm>
1 adjacent_find 查找重复的元素 2 find_if 查找符合条件的第一个元素 3 find_if_not 查找不符合条件的第一个元素 4 for_each 可以遍历每一个元素 5 pa ...
- Windows窗口消息大全(转)
Windows窗口消息大全,全不全自己看 ////////////////////////////////////////////////////////////////////////// #inc ...
- SGU 134.Centroid( 树形dp )
一道入门树dp, 求一棵树的重心...我是有多无聊去写这种题...傻X题写了也没啥卵用以后还是少写好.. ----------------------------------------------- ...
- python strip()函数介绍
函数原型 声明:str为字符串,s为要删除的字符序列 str.strip(s) 删除str字符串中开头.结尾处,位于 s删除序列的字符 str.lstrip(s) 删除str ...
- JS行合并处理方法
//行合并 function _w_table_rowspan(col){ _w_table_firsttd = ""; _w_table_currenttd = "&q ...
- Auto login to your computer
in run dialog, type in following words and enter Rundll32 netplwiz.dll,UsersRunDll On user account d ...
- 有关extern的用法
1.引言 C++语言的创建初衷是“a better C”,但是这并不意味着C++中类似C语言的全局变量和函数所采用的编译和连接方式与C语言完全相同.作为一种欲与C兼容的语言, C++保留了一部分过程式 ...
- 面试常用算法——Longest Palindromic Substring(最长回文子串)
第一种: public static void main(String[] args) { String s = "abcbaaaaabcdcba"; int n,m; Strin ...