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.
利用二叉排序树的性质,可以很好地解决这道题。
solution:
TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) {
if (root == NULL || p == NULL || q == NULL)
return NULL;
if (root->val > p->val && root->val > q->val)
return lowestCommonAncestor(root->left, p, q);
else if (root->val < p->val && root->val < q->val)
return lowestCommonAncestor(root->right, p, q);
else
return root;
}
如果只是一颗普通的二叉树呢?
solution:
TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) {
if (!root)
return NULL;
if (root == p || root == q)
return root;
TreeNode *L = lowestCommonAncestor(root->left, p, q);
TreeNode *R = lowestCommonAncestor(root->right, p, q);
if (L && R)
return root;
return L ? L : R;
}
来源:Lowest Common Ancestor of a Binary Tree Part I
如果二叉树的结点存在指向父结点的指针,问题可以转化为求两个单链表的相交结点。
solution:
struct TreeNode {
int val;
TreeNode *left;
TreeNode *right;
TreeNode *parent;
};
int getHeight(TreeNode *p)
{
int height = ;
while (p)
{
height++;
p = p->parent;
}
return height;
}
TreeNode* lowestCommonAncestor(TreeNode *p, TreeNode *q)
{
int h1 = getHeight(p);
int h2 = getHeight(q);
if (h1 > h2)
{
swap(h1, h2);
swap(p, q);
}
int dh = h2 - h1;
for (int h = ; h < dh; ++h)
q = q->parent;
while (p && q)
{
if (p == q)
return p;
p = p->parent;
q = q->parent;
}
return NULL;
}
来源:Lowest Common Ancestor of a Binary Tree Part II
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 BS ...
- [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】#235. Lowest Common Ancestor of a Binary Search Tree
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...
随机推荐
- https的秘钥公钥以及之间的会话流程
一 共享秘钥 1.1 概念 共享秘钥和我们生活中同一把锁的钥匙概念类似,对同一把锁来说,加锁时使用什么钥匙,解锁也必须使用同样的钥匙. 1.2 共享秘钥在HTTP传输中的缺点 以共享密钥方式加密时 ...
- I/O流之--转换流:InputStreamReader 和InputStreamWriter
I/O流之--转换流:InputStreamReader 和InputStreamWriter 分类: java2014-07-01 15:30 815人阅读 评论(0) 收藏 举报 目录(?)[ ...
- 【论文笔记】张航和李沐等提出:ResNeSt: Split-Attention Networks(ResNet改进版本)
github地址:https://github.com/zhanghang1989/ResNeSt 论文地址:https://hangzhang.org/files/resnest.pdf 核心就是: ...
- Android 程序代码进行代码混淆
1.在Eclipse项目包下的project.properties文件中加入proguard.config=${sdk.dir}/tools/proguard/proguard-android.txt ...
- 利用Ajax实现异步请求
Ajax 1.课程引入 静态网站和动态网站都是同步的,但同步方式有缺点:页面请求响应式阻塞,影响用户体验 为了解决这个问题,可以通过变通的手段实现页面的局部更新(隐藏帧),由于隐藏 ...
- Salesforce学习 | 系统管理员Admin如何添加用户
作为世界排名第一的CRM云计算软件,不管的是500强还是中小企业,越来越多的公司都选择使用Salesforce来分享客户信息,管理和开发具有更高收益的客户关系.Salesforce Administr ...
- stand up meeting 12/25/2015 & weekend 12/26/2015~12/27/2015
part 组员 工作 工作耗时/h 明日计划 工作耗时/h UI 冯晓云 在pdf阅读页面添加生词本显示:UI美化 6 完善显示 ...
- asp.net mvc 接收jquery ajax发送的数组对象
<script type="text/javascript"> $(function () { var obj = { name: "军需品", m ...
- mapstruct使用详解
我们都知道,随着一个工程的越来越成熟,模块划分会越来越细,其中实体类一般存于 domain 之中,但 domain 工程最好不要被其他工程依赖,所以其他工程想获取实体类数据时就需要在各自工程写 mod ...
- 关于vue切换用户,路由表不更新问题
简介 我想很多同学在项目中可能会遇到类似的问题,然后一顿操作,发现结果不尽人意.于是查阅各种资料,走进很多坑(可能你阅读的这篇随笔也是个坑).接下来我所描述的是关于我使用不同权限的用户切换登陆后,需要 ...