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;
* struct TreeNode *left;
* struct TreeNode *right;
* };
*/
struct TreeNode* lowestCommonAncestor(struct TreeNode* root, struct TreeNode* p, struct TreeNode* q) {
if(!root && !p && !q)
return ;
if(root->val == p->val)
return p;
if(root->val == q->val)
return q;
if((root->val > p->val && root->val < q->val)||(root->val < p->val && root->val > q->val))
return root;
if(root->val > p->val && root->val > q->val)
return lowestCommonAncestor(root->left,p,q);
if(root->val < p->val && root->val < q->val)
return lowestCommonAncestor(root->right,p,q);
return ;
}
Lowest Common Ancestor of a Binary Search Tree的更多相关文章
- [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] 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 ...
- [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 ...
- [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 ...
随机推荐
- Spring MVC 原理小结
主要由DispatcherServlet.处理器映射.处理器.视图解析器.视图组成 1.DispatcherServlet接收到一个HTTP请求,根据对应配置文件中的处理机映射,找到处理器(Han ...
- 怎么样使用yum来安装、卸载jdk
安装好的CentOS会自带OpenJdk,用命令 java -version ,会有下面的信息: java version "1.6.0"OpenJDK Runtime Envi ...
- 转载文章----初识Ildasm.exe——IL反编译的实用工具
转载地址http://www.cnblogs.com/yangmingming/archive/2010/02/03/1662307.html Ildasm.exe 概要:(路径:C:\Program ...
- 示例详解:UIScrollview 与 Autolayout 的那点事
前言 自从写了介绍Masonry那篇文章以后 就一直有人对UIScrollView的那个例子不是很理解 UIView *container = [UIView new]; [scrollView ad ...
- substring -----截取字符串
var str = "0123456789"; substring alert(str.substring(0));------------"0123456789&quo ...
- 让你脱离google不能访问的烦恼
大陆封了google已有20多天了,给开发者带来了许多不便.只需两步让你的google可以使用: 1.设置hosts: 访问:https://git.oschina.net/kawaiiushio/m ...
- 按要求编写Java应用程序: (1)编写西游记人物类(XiYouJiRenWu) 其中属性有:身高(height),名字(name),武器(weapon) 方法有:显示名字(printName),显示武器(printWeapon) (2)在主类的main方法中创建二个对象:zhuBaJie,sunWuKong。并分别为他 们的两个属性(name,weapon)赋值,最后分别调用printName,
package com.hanqi.test; public class xiyoujirenwu { private double height;// 身高 private String name; ...
- 解猜数字(XAXB)
我的想法是:把所有备选答案当做正确答案和猜的数字对比,如果得出XAXB和给出的XAXB相同则保留 代码 ; ; List<string> number = new List<stri ...
- SQL SERVER 查看数据库表的字段类型,是否允许为NULL,默认值,主键等
)-- 表名 set @table_name='bqcform101' --============表结构 select 类别,表名or字段名,描述,字段类型,是否自增,允许为NULL,默认值 fro ...
- 【Unity】改变向量的方向而不改变其大小
最近在做一个打砖块游戏时遇到一个小问题,就是小球有可能会在左右两个边界之间做循环往返运动而导致游戏无法继续进行下去,于是我打算让小球在垂直撞向边界时改变一下方向,但是速度不变,尝试了一些方法但是没有达 ...