LintCode:最近公共祖先
2016-09-01 00:21
423 查看
LintCode:最近公共祖先
""" Definition of TreeNode: class TreeNode: def __init__(self, val): self.val = val self.left, self.right = None, None """ import copy class Solution: """ @param root: The root of the binary search tree. @param A and B: two nodes in a Binary. @return: Return the least common ancestor(LCA) of the two nodes. """ def lowestCommonAncestor(self, root, A, B): # write your code here if root == None: return None if root == A or root == B: return root left = self.lowestCommonAncestor(root.left, A, B) right = self.lowestCommonAncestor(root.right, A, B) if left and right: return root elif left: return left elif right: return right else: return None
相关文章推荐
- lintcode-最近公共祖先-88
- LintCode 最近公共祖先
- lintcode lowest-common-ancestor 最近公共祖先
- lintcode-最近公共祖先
- LintCode-最近公共祖先
- LintCode-88.最近公共祖先
- lintcode :最近公共祖先
- lintcode--最近公共祖先
- CodeVs.1036 商务旅行 ( LCA 最近公共祖先 )
- CodeVs.2370 小机房的树 ( LCA 倍增 最近公共祖先)
- 【Codeforces Round 362 (Div 2)C】【STL-map 最近公共祖先思想】Lorenzo Von Matterhorn 数域二叉树的路径权值变更查询
- Codeforces Gym 101142 G. Gangsters in Central City (最近公共祖先)
- 【最近公共祖先】【块状树】CODEVS 1036 商务旅行
- LintCode:M-最近祖先
- LCA(最近公共祖先 Tarjan) CodeVs-2370-小机房的树
- POJ 1470 Closest Common Ancestors【最近公共祖先LCA】
- 程序员面试金典——最近公共祖先
- LCA最近公共祖先(倍增版)
- hihocoder #1062 : 最近公共祖先·一(小数据量 map+set模拟+标记检查 *【模板】思路 )
- TOJ 3033 ZOJ 1141 POJ 1470 Closest Common Ancestors / 最近公共祖先