[LeetCode235]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.
二叉搜索树求两个结点最近的祖先
分类:Tree
代码:
/**
* 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(p->val < root->val && q->val < root->val)
return lowestCommonAncestor(root->left,p,q);
if(p->val > root->val && q->val > root->val)
return lowestCommonAncestor(root->right,p,q);
return root;
}
};
[LeetCode235]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 ...
随机推荐
- 使用ILmerge合并Exe、Dll文件的帮助类
原文:使用ILmerge合并Exe.Dll文件的帮助类 using System; using System.Collections.Generic; using System.Text; using ...
- Replace - with an en dash character (–, –) ?
这个安卓开发过程中eclipse的提示,新浪网友给出这个解决方法:http://blog.sina.com.cn/s/blog_5ea8670101015dgk.html 太笨了. 看看stacko ...
- SQL Syscolumns
每个表和视图中的每列在表中占一行,存储过程中的每个参数在表中也占一行.该表位于每个数据库中. 列名 数据类型 描述 name sysname 列名或过程参数的名称. id int 该列所属的表对象 I ...
- Java中定时器的使用
import java.text.SimpleDateFormat; import java.util.Date; import java.util.Timer; import java.util.T ...
- pig中使用的一些实例语法
在pig中, dump和store会分别完毕两个MR, 不会一起进行 1:载入名用正則表達式: LOAD '/user/wizad/data/wizad/raw/2014-0{6,7-0,7-1,7- ...
- Ubuntu 12.04 安装 Tomcat8 遇到的问题
问题: :/tomcat8/bin$ sudo ./configtest.sh Using CATALINA_BASE: /home/yyb/android/tomcat8 Using CATAL ...
- cocos2dx-lua牧场小游戏(一)
环境: cocos2dx-3.0rc2, xcode5.0 一.lua项目建立參考 http://blog.csdn.net/daydayup_chf/article/details/249641 ...
- 数据库采用多表连接查询,对应javaBean文件连接方式
在一个Web项目中,只要是存在数据库就一定会有JavaBean文件.一个JavaBean文件会对应一张数据库中的表,供dao中的代码来调用用来存取数据.我们都知道,在数据库设计的时候,如果A.B两张表 ...
- HorizontalScrollView做页卡的一个小记录
用HorizontalScrollView做页卡,实现一个如下图的效果:
- sql语句用'in'执行多条语句时候,执行错误的解决方法
一般报错是出现,无法将nvarchar类型转换为int类型 这是因为 SqlParameter 带参数 是不能用 , 分割的. 第一种解决方法就是 不用 SqlParameter 带参数的s ...