#-*- coding: UTF-8 -*-
# Definition for a binary tree node.
# class TreeNode(object):
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None
class Solution(object):
def lowestCommonAncestor(self, root, p, q):
"""
:type root: TreeNode
:type p: TreeNode
:type q: TreeNode
:rtype: TreeNode
"""
maxp=p if p.val>q.val else q
minp=q if q.val<p.val else p
print maxp.val,minp.val
if maxp.val>=root.val and minp.val<=root.val:
return root.val
elif minp.val>root.val:
return self.lowestCommonAncestor(root.right,maxp,minp)
elif maxp.val<root.val:
return self.lowestCommonAncestor(root.left,maxp,minp)
相关文章
- 【LeetCode】108. Convert Sorted Array to Binary Search Tree 解题报告 (Java & Python)
- Lowest Common Ancestor of a Binary Search Tree、Lowest Common Ancestor of a Binary Search Tree
- Lowest Common Ancestor in Binary Tree
- 235. Lowest Common Ancestor of a Binary Search Tree
- leetcode之Lowest Common Ancestor of a Binary Search Tree
- LeetCode Lowest Common Ancestor of a Binary Search Tree
- [LeetCode] Lowest Common Ancestor of a Binary Search Tree
- [LeetCode] Lowest Common Ancestor of a Binary Search Tree
- LeetCode: Lowest Common Ancestor of a Binary Search Tree 解题报告
- Java [Leetcode 235]Lowest Common Ancestor of a Binary Search Tree