Simple Python Recursive Solution


  • 0
    W

    '''
    def lowestCommonAncestor(self, root, p, q):

       if not root or not p or not q:
           return None
    
       if p.val < root.val and q.val < root.val:
           return self.lowestCommonAncestor(root.left, p, q)
       elif p.val > root.val and q.val > root.val:
           return self.lowestCommonAncestor(root.right, p, q)
       return root

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.