力扣235. 二叉搜索树的最近公共祖先

原题

1 class Solution:
2     def lowestCommonAncestor(self, root: 'TreeNode', p: 'TreeNode', q: 'TreeNode') -> 'TreeNode':
3         if p.val < root.val and q.val < root.val: 
4             return self.lowestCommonAncestor(root.left,p,q)
5         elif p.val > root.val and q.val > root.val:
6             return self.lowestCommonAncestor(root.right,p,q)
7         else:return root

 

posted @ 2021-02-08 13:32  凝视深空  阅读(51)  评论(0编辑  收藏  举报