【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 BST.
According to the definition of LCA on Wikipedia: “The lowest common ancestor is defined between two nodes v and w as the lowest node in T that has both v and w as descendants (where we allow a node to be a descendant of itself).”
_______6______
/ \
___2__ ___8__
/ \ / \
_4
/ \
For example, the lowest common ancestor (LCA) of nodes 2
and 8
is 6
. Another example is LCA of nodes 2
and 4
is 2
, since a node can be a descendant of itself according to the LCA definition.
思路:
利用BST的性质,若待查找两个结点的数值都小于当前结点则向左边查找(若有一个等于当前结点则当前结点就是其最小的公共结点,结束查找),反之向右边查找(同理),若一个大于一个小于,则当前结点就是其最小的公共结点,结束查找。
C++:
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
private:
TreeNode *l, *r, *ret;
public: void rec(TreeNode *root)
{
int v = root->val;
if(l->val <= v && r->val <= v)
{
if(v == l->val || v == r->val)
{
ret = root;
return ;
} if(root->left != )
rec(root->left);
}
else if(l->val < v && v < r->val)
{
ret = root;
return ;
}
else if(l->val >= v && r->val >= v)
{
if(v == l->val || v == r->val)
{
ret = root;
return ;
} if(root->right != )
rec(root->right);
}
} TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) {
if(root == )
return ; if(p->val < q->val){
l = p;
r = q;
}
else{
l = q;
r = p;
} ret = ; rec(root); return ret;
}
};
【LeetCode 235】Lowest Common Ancestor of a Binary Search Tree的更多相关文章
- 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 ...
- 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 236】Lowest Common Ancestor of a Binary Tree
Given a binary tree, find the lowest common ancestor (LCA) of two given nodes in the tree. According ...
- LeetCode OJ: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 235_二叉搜索树】Lowest Common Ancestor of a Binary Search Tree
解法一:递归 TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) { if (root == NULL | ...
- LeetCode算法题-Lowest Common Ancestor of a Binary Search Tree
这是悦乐书的第197次更新,第203篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第59题(顺位题号是235).给定二叉搜索树(BST),找到BST中两个给定节点的最低共 ...
- 【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 ...
- 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
235. Lowest Common Ancestor of a Binary Search Tree Easy Given a binary search tree (BST), find the ...
随机推荐
- android-exploitme(二):安装apk熟悉测试环境
今天我们来熟悉测试环境: 1. 下载server代码,并运行 git clone https://github.com/SecurityCompass/LabServer.git 2. 这个serve ...
- IOS 的loadView 及使用loadView中初始化View注意的问题。(死循环并不可怕)
在XCode 4.2后,我基本上的应用都不使用Xib文件了,虽然xib文件有很多好趣,可以快速免代码构建视窗,可以减少好多代码构建带来的麻烦,其实能用xib还是不错的,主要是我的机器打开xib来编辑时 ...
- POJ3252——Round Number(组合数学)
Round Numbers DescriptionThe cows, as you know, have no fingers or thumbs and thus are unable to pla ...
- nodpad++正则替换
则表达式是一个查询的字符串,它包含一般的字符和一些特殊的字符,特殊字符可以扩展查找字符串的能力,正则表达式在查找和替换字符串的作用不可忽视,它 能很好提高工作效率. EditPlus的查找,替换,文件 ...
- Java基础复习之一篇:关健字,标识符,注释,常量,进制转换,变量,数据类型,数据类型转换
1.关健字 1.1.被Java语言赋予特定意义的单词(如:class,interface,public ,static) 1.2.全部是小写 1.3.注意事项(goto和const作为关健字保留起来) ...
- Debugging JTAG Connectivity Problems
2013-12-04 22:34:26 转自:http://processors.wiki.ti.com/index.php/Debugging_JTAG_Connectivity_Problems ...
- java验证码-汉字验证码
今天整理了一个java实现的汉字输入验证码 主要包含两个类,一个是生成验证码,一个是判断验证码输入是否正确 实现原理非常简单,将汉字和干扰线生成图片并将汉字保存到session,前台获取每次生成验证码 ...
- 命令 tar & zip
安装zip yum install -y unzip zip: tar-c: 建立压缩档案-x:解压-t:查看内容-r:向压缩归档文件末尾追加文件-u:更新原压缩包中的文件 这五个是独立的命令,压缩解 ...
- 对Java“一切皆对象”的理念的理解
在从HelloWorld到面向对象中,我们将int, float, double, boolean等称为基本类型(primitive type),也就是特殊的类.我们可以将一个整数理解称为一个int类 ...
- Create a unit test project
https://msdn.microsoft.com/en-us/library/hh598957.aspx Unit tests often mirror the structure of the ...