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 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__/ \ / \0 _4 7 9/ \3 5
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.
题目解答:这个题目非常简单,就是求在二叉搜索树中,两个子节点的最近公共祖先。
首先说明一下,在二叉排序树中,是没有取值相同的元素的。另外,它还具有以下特点:
代码如下:
/**
* 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 {
public:
TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) {
if((root == NULL) || (p == NULL) || (q == NULL))
return NULL;
TreeNode * curNode = root;
if(p -> val > q -> val)
{
while(curNode != NULL)
{
if(curNode -> val > p -> val)
{
curNode = curNode -> left;
}
else if(curNode -> val < q -> val)
{
curNode =curNode -> right;
}
else
return curNode;
}
}
else if(p -> val < q -> val)
{
while(curNode != NULL)
{
if(curNode -> val > q -> val)
{
curNode = curNode -> left;
}
else if(curNode -> val < p -> val)
{
curNode =curNode -> right;
}
else
return curNode;
}
}
return NULL;
}
};
转载于:https://www.cnblogs.com/CodingGirl121/p/5414225.html
Leetcode题目:Lowest Common Ancestor of a Binary Search Tree相关推荐
- [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 ...
- leetcode 235. Lowest Common Ancestor of a Binary Search Tree | 235. 二叉搜索树的最近公共祖先(哈希表)
题目 https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-search-tree/ 题解 哈希表解法思路来自左程云< ...
- Lowest Common Ancestor of a Binary Search Tree a Binary Tree
235. Lowest Common Ancestor of a Binary Search Tree 题目链接:https://leetcode.com/problems/lowest-common ...
- 235 Lowest Common Ancestor of a Binary Search Tree
题目 235 Lowest Common Ancestor of a Binary Search Tree 因为是binary search tree,因此利用没个节点的值进行二分查找即可复杂度O(h ...
- [CareerCup] 4.7 Lowest Common Ancestor of a Binary Search Tree 二叉树的最小共同父节点
4.7 Design an algorithm and write code to find the first common ancestor of two nodes in a binary tr ...
- 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 ...
- 235. Lowest Common Ancestor of a Binary Search Tree(Tree-Easy)
转载请注明作者和出处: http://blog.csdn.net/c406495762 Given a binary search tree (BST), find the lowest common ...
- Leet Code OJ 235. Lowest Common Ancestor of a Binary Search Tree [Difficulty: Easy]
题目: Given a binary search tree (BST), find the lowest common ancestor (LCA) of two given nodes in th ...
- LeetCode:235. 二叉搜索树的最近公共祖先(Lowest Common Ancestor of a Binary Search Tree)
二叉搜索树性质: 1.任意节点node,其左子树中的val不大于node.val,其右子树中的val不小于node.val. 2.不同的二叉搜索树可以代表同一组值的集合 3.二叉搜索树的基本操作和树的 ...
最新文章
- sql server性能分析--执行sql次数和逻辑次数
- 如何学习项目管理?项目管理工具有哪些?
- Android 事件与事件监听器
- sum除以count 和avg 的区别_EXCEL函数之计数COUNT系列
- shell脚本中一些特殊符号
- 为什么操作系统会有随机性
- 线程中发送消息阻塞问题解决
- Oracle 12C CDB、PDB常用管理命令
- Django syncdb mysql error on localhost - (1045, Access denied for user 'ODBC'@'
- java ioc和aop的含义_Spring核心IoC和AOP的理解
- 优化 Tengine HTTPS 握手时间
- 个人计算机硬件构成的叙述正确的是,2010年职称计算机考试模拟试题及答案(计算机基础)2...
- 重磅|前浪、后浪 一起迎接风口! BCS 2020向全球发起议题征集
- 极光短信验证码JAVA_Android如何集成极光短信验证
- sublime运行python_sublime交互执行python文件方法
- 苹果妙控鼠标二代(Magic Mouse 2 )如何连接到 Window 10系统
- ppt thinkcell-Thinkcell: 一款强大的专业图表制作工具
- gatk过滤_GATK Hard-filter 过滤变异结果推荐阈值
- 二阶常系数非齐次线性微分方程特解的设定规则
- 【总结】新产品开发阶段的名词 EVT/DVT/PVT/MP
热门文章
- JSP的MVC模式(JavaBean、Servlet、JSP)
- php有没有dao层,php框架开发四(DAO层)_PHP教程
- Linux下Grub命令配置详解
- CSS定义表格边框大全(细线/虚线/点线)
- 智能一代云平台(三十七):Java技术栈
- 英语总结系列(十八):六月一波三折的英语
- TensorFlow 1.9.0正式版来了!新手指南全新改版,支持梯度提升树估计器
- 27个澳洲年轻人,重演了少年马云的一段奇遇
- websocket包解析
- 移动webAPP前端开发技巧汇总