236 二叉树的最近公共祖先

 

 

class Solution:
    def lowestCommonAncestor(self, root: 'TreeNode', p: 'TreeNode', q: 'TreeNode') -> 'TreeNode':
        if not root or root==p or root==q:return root
        left=self.lowestCommonAncestor(root.left,p,q)
        right=self.lowestCommonAncestor(root.right,p,q)
        if not left: return right
        if not right: return left
        return root

 

posted @ 2021-03-28 20:21  小千北同学超爱写代码  阅读(46)  评论(0编辑  收藏  举报