【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 ...
随机推荐
- CreateFile,WriteFile,ReadFile
注意: CreateFile 跟 fopen 不同,打开文件时不区分 文本方式 或 二进制 方式 ReadFile 或 WriteFile 都是对二进制数据进行操作 HANDLE WINAPI Cre ...
- IOS系统中使用zepto的live事件绑定不了的一个办法
IOS系统中使用zepto的live事件绑定不了的一个办法: 对事件对象添加样式:cursor:pointer
- C# 中间语言、CLR、CTS、CLS
c# 中间语言.CLR.CTS.CLS IL中间语言,区分大小写 中间语言的主要特征: 1. 面向 对象和使 用接口 2. 值类型和引 用类 型之间的 显 著差异 3. 强 数据类型化 4. 使 ...
- c#对文件进行MD5加密校验
public static string GetFileMd5Hash(string strFileFullPath) { // Create a new instance of the MD5Cry ...
- Linux-0.00运行环境搭建【转】
转自:http://blog.csdn.net/rosetta/article/details/8933240 这里的Linux-0.00由Linus Torvalds写的Linux最初版本,只是打印 ...
- 使用C#开发ActiveX控件(新) 转 http://www.cnblogs.com/yilin/p/csharp-activex.html
前言 ActiveX控件以前也叫做OLE控件,它是微软IE支持的一种软件组件或对象,可以将其插入到Web页面中,实现在浏览器端执行动态程序功能,以增强浏览器端的动态处理能力.通常ActiveX控件都是 ...
- 了解ThinkPHP(一)
1.项目开发,中,会遇到的问题: 1). 多人开发项目,分工不合理,(html php mysql) 2). 代码风格不一样,后期维护十分困难 3). 项目生命周期十分短,项目生命没有延续性, ...
- PHP empty函数判断0返回真还是假?
最近项目中,遇到一个字段是 “是否启用”值为0,1 在查询时没想就写了 if ( isset($args_array['useFlg']) && !empty($args_array[ ...
- CollectionBase类
在命名空间System.Collections下的CollectionBase类 The CollectionBase class exposes the interfaces IEnumerable ...
- javascript高级编程运用
一//各种尺寸 (size) s += “\r\n网页可见区域宽:“+ document.body.clientWidth; s += “\r\n网页可见区域高:“+ document.body.cl ...