python - lca

  • 0
    # Definition for a binary tree node.
    # class TreeNode(object):
    #     def __init__(self, x):
    #         self.val = x
    #         self.left = None
    #         self.right = None
    class Solution(object):
        def lowestCommonAncestor(self, root, p, q):
            :type root: TreeNode
            :type p: TreeNode
            :type q: TreeNode
            :rtype: TreeNode
            if not root:
                return None
            if (p.val == root.val) or (q.val == root.val):
                return root
            if (p.val < root.val and q.val > root.val) or (p.val > root.val and q.val < root.val):
                return root
            if (p.val < root.val) and (q.val < root.val):
                return self.lowestCommonAncestor(root.left, p, q)
            if (p.val > root.val) and (q.val > root.val):
                return self.lowestCommonAncestor(root.right, p, q)

Log in to reply

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