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 ...
随机推荐
- lr集合点
集合点:rendezvous point =====>>loadrunner的虚拟用户中,并发concurrent\ 同时simultaneous的区别: concurrent并发:指虚拟 ...
- kafka的基本体系结构
使用场景 大数据:数据量和速率激增,数据类型越来越复杂 应用开发:消息引擎,应用解耦,分布式存储,流处理 Kafka的体系结构 topic : 主题(消息的逻辑分类) 客户端: 细分为生产者(朝主题发 ...
- 算法:模拟退火(基于c++程序)
一 什么是模拟退火算法? 所谓退火,其实是金属冶炼的一个名词.比如加工一把刀,我们通常是把材料加工到很高的一个温度,加以锤炼.之后慢慢的将温度降下来,如果我们降温的控制比较好的话,那么金属里面的原子就 ...
- AJ学IOS 之二维码学习,快速打开相机读取二维码
AJ分享,必须精品 上一篇文章写了怎么生成二维码,这儿就说说怎么读取吧,反正也很简单,iOS封装的太强大了 步骤呢就是这样: 读取二维码需要导入AVFoundation框架#import <AV ...
- 复习python的__call__ __str__ __repr__ __getattr__函数 整理
class Www: def __init__(self,name): self.name=name def __str__(self): return '名称 %s'%self.name #__re ...
- Django开发文档-域用户集成登录
项目概述: 一般在企业中,用户以WINDOWS的域用户统一的管理,所以以Django快速开发的应用,不得不集成AD域登录. 网上一般采用django-python3-ldap的库来做集成登录,但是本方 ...
- 【题解】POJ3041 Asteroids - 图论 - 二分图匹配
声明:本博客所有题解都参照了网络资料或其他博客,仅为博主想加深理解而写,如有疑问欢迎与博主讨论✧。٩(ˊᗜˋ)و✧*。 POJ3041 Asteroids 题目描述 假如你现在正处在一个 \(N*N\ ...
- ADO.NET(二)
对Command的拓展延伸 执行SQL语句. Command 对象需要取得将要执行的SQL语句,通过调用该类的多种方法,向数据库提交SQL语句. ExecuteNonQuery(),ExecuteR ...
- seo 回忆录百度基本概念(一)
前言 我以前的博客自己做的seo,现在拿来和大家一起交流,是白帽哈,黑帽的不敢发,也不敢学[微笑]. 正文 为什么做seo 做seo说到底就是为了排名.为什么需要排名呢?因为现在人比较懒,只会去查看第 ...
- pytorch 中交叉熵损失实现方法