leetcode——235. 二叉搜索树的最近公共祖先

class Solution:
    def lowestCommonAncestor(self, root: 'TreeNode', p: 'TreeNode', q: 'TreeNode') -> 'TreeNode':
        if p.val>q.val:
            p,q=q,p
        if q.val<root.val:
            return self.lowestCommonAncestor(root.left,p,q)
        elif p.val>root.val:
            return self.lowestCommonAncestor(root.right,p,q)
        else:
            return root
执行用时 :88 ms, 在所有 python3 提交中击败了91.02%的用户
内存消耗 :18 MB, 在所有 python3 提交中击败了5.48%的用户
 
——2019.11.19
posted @ 2019-11-19 16:26  欣姐姐  阅读(72)  评论(0编辑  收藏  举报