Python recursive solution.


  • 0
    T

    I love python! the solution itself reads like a psuedo code.

    def lowestCommonAncestor(self, root, p, q):
            if not root: return None
            if root == p or root == q: return root
            left = self.lowestCommonAncestor(root.left, p, q)
            right = self.lowestCommonAncestor(root.right, p, q)
            if left and right:
                return root
            else:
                return left or right

Log in to reply
 

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