#235. Lowest Common Ancestor of a Binary Search Tree 题目链接
因为题目给出的二叉树是二叉排序树,所以可以利用这一特性方便求解
# 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
"""
while (root.val - p.val) * (root.val - q.val) > 0:
root = root.left if p.val < root.val else root.right
return root