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 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.
通过观察可以发现,如果p和q分别位于root的两侧,那么root就是他们的LCA。否则的话,判断一下LCA应该是在左边还是在右边。注意L11 L13的return不能缺,因为这里并不是在调用这个函数,而是在递归定义。
def lowestCommonAncestor(self, root, p, q):
"""
:type root: TreeNode
:type p: TreeNode
:type q: TreeNode
:rtype: TreeNode
"""
if (p.val <= root.val and q.val >= root.val) or (p.val >= root.val and q.val <= root.val):
return root
elif p.val < root.val:
return self.lowestCommonAncestor(root.left, p, q)
else:
return self.lowestCommonAncestor(root.right, p, q)
leetcode 235. Lowest Common Ancestor of a Binary Search Tree的更多相关文章
- 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 二叉搜索树的最近公共祖先
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 ...
- (easy)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 ...
- Java [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 ...
- Leetcode 235 Lowest Common Ancestor of a Binary Search Tree 二叉树
给定一个二叉搜索树的两个节点,找出他们的最近公共祖先,如, _______6______ / \ ___2__ ___8__ / \ / \ 0 4 7 9 / \ 3 5 2和8的最近公共祖先是6, ...
- Java for LeetCode 235 Lowest Common Ancestor of a Binary Search Tree
递归实现如下: public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) { if(p.val>ro ...
- 【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 ...
随机推荐
- sublime和python
<转载> 使用 Sublime 工具时报Decode error - output not utf-8解决办法 打开Python.sublime-build文件,并添加"en ...
- 048医疗项目-模块四:采购单模块—采购单受理(Dao,Service,Action三层)
需求: 我们之前把采购单交给监督单位审核了,审通过的采购单就要受理.供货商决定采购单发不发货. 说明: 我们要查的就是登录的供货商的要提供的采购药品,我们查看的是采购单详细表,至于查询条件我们用的是就 ...
- Codevs 1860 最大数 string大法好,STL万岁。。
题目描述 Description 设有n个正整数(n≤20),将它们联接成一排,组成一个最大的多位整数. 输入描述 Input Description 第一行一个正整数n. 第二行n个正整数,空格隔开 ...
- JS对异步循环使用递归
场景:有一个函数接收一个URL的数组,要求依次下载每个文件直到所有文件被成功下载. 如果API是同步的,很容易使用一个循环来实现 var downloadAllSync = function(urls ...
- oracle:db-link使用
二个oracle instance,如果需要在一个instance上,直接查询另一个instance上的数据,就要用到db-link 创建: create public database link 链 ...
- SQLServer(MSSQL)、MySQL、SQLite、Access相互迁移转换工具 DB2DB v1.3
最近公司有一个项目,需要把原来的系统从 MSSQL 升迁到阿里云RDS(MySQL)上面.为便于测试,所以需要把原来系统的所有数据表以及测试数据转换到 MySQL 上面.在百度上找了很多方法,有通过微 ...
- c:forEach 标签中varStatus的用法
c:forEach varStatus属性 current 当前这次迭代的(集合中的)项index 当前这次迭代从 0 开始的迭代索引count 当前这次迭代从 1 开始的迭代计数first 用来 ...
- 【REST WCF】30分钟理论到实践
先来点理论知识,来自 http://www.cnblogs.com/simonchen/articles/2220838.html 一.什么是Rest REST软件架构是由Roy Thomas Fie ...
- jquery图片轮播效果(unslider)
今天做网站(住建局网站)需要用到图片轮播,刚开始想借鉴DTCMS上的,查看CSS与页面代码,呵呵,不复杂,直接复制过来,结果调整半天,页面还是各种乱,没办法,网上找一个吧,于是找到了今天要说的这货un ...
- 20160205 - Windows 10 家庭版没有组策略
问题描述:买笔电自带的正版系统,有一个需要使用gpedit.msc,发现并不存在. 解决办法:升级到 Windows 10 专业版