【LeetCode 235_二叉搜索树】Lowest Common Ancestor of a Binary Search Tree

解法一:递归
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);
return root;
}
解法二:迭代
TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q)
{
if (root == NULL || p == NULL || q == NULL)
return NULL; while (true) {
if (root->val < p->val && root->val < q->val)
root = root->right;
else if (root->val > p->val && root->val > q->val)
root = root->left;
else
break;
}
return root;
}
【LeetCode 235_二叉搜索树】Lowest Common Ancestor of a Binary Search Tree的更多相关文章
- 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 ... 
- 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 ... 
- 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 ... 
- [geeksforgeeks] Lowest Common Ancestor in a Binary Search Tree.
		http://www.geeksforgeeks.org/lowest-common-ancestor-in-a-binary-search-tree/ Lowest Common Ancestor ... 
- 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 ... 
- 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] 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 ... 
- [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 ... 
- [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 ... 
随机推荐
- UVa 11134 传说中的车
			https://vjudge.net/problem/UVA-11134 题意:在n*n的棋盘上放n个车,使得任意两个车不相互攻击,且第i个车在一个给定的矩形Ri之内.用4个整数xli,yli,xri ... 
- java List中相同的数据合并到一起
			值好最后一天岗,写个小程序 List<Map<Object,Object>> mapList = new ArrayList<Map<Object,Object&g ... 
- Oncomine: 一个肿瘤相关基因研究的数据库--转载
			如果你获得了一个肿瘤差异表达基因,想研究其是否可作为某种肿瘤的潜在标志物和靶点,又怕做实验会得到阴性结果,浪费时间和金钱,这时候你就应该想到Oncomine数据库了(www.oncomine.org) ... 
- resource not found : rosbridge_server
			1.放到src下,改名字为rosbridge_server,编译catkin_make git clone https://github.com/RobotWebTools/rosbridge_sui ... 
- html禁止文本输入框记录输入记录,单击input出现输入过的记录
			其实方法很简单,只需要在input文本输入框中加一条autocomplete="off"属性即可. <input type="text" name=&qu ... 
- iOS Socket编程-C语言版(TCP)
			. TCP Socket编程 TCP是面向连接的,安全可靠的传输层协议.TCP的程序基本框架设计图: TCP的程序基本框架设计图.jpg 注意:Socket通信一定有要服务端和客户端. 1.1 TCP ... 
- 更新自带pip
			想安装docker-compose发现居然找不到pip curl https://bootstrap.pypa.io/get-pip.py | python 直接sudo不行.还是提示权限不够. su ... 
- Java 常用对象-String类
			2017-11-02 20:02:06 String:代表字符串.Java 程序中的所有字符串字面值(如 "abc" )都作为此类的实例实现. 字符串是常量:它们的值在创建之后不能 ... 
- tomcat日志切割和定期删除
			tomcat日志切割和定期删除 在tomcat的软件环境中,如果我们任由日志文件无限增长,总有一天会将磁盘占满的(废话).特别是在日志文件增长速度很快的一些情况下,按日志切割日志文件并删除,就是一件很 ... 
- vs2010打包安装
			[WinForm] VS2010发布.打包安装程序(超全超详细) 2017年02月17日 21:47:09 y13156556538 阅读数:16487更多 个人分类: C#winform 1. ... 
