#-*- 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)