Untitled

mail@pastecode.io avatar
unknown
plain_text
a month ago
435 B
1
Indexable
Never
class Solution(object):
    def lowestCommonAncestor(self, root, p, q):
        """
        :type root: TreeNode
        :type p: TreeNode
        :type q: TreeNode
        :rtype: TreeNode
        """  
        def lca(node):
            if node:
                l, r = lca(node.left), lca(node.right)
                curr = node == p or node == q
                return node if (l and r) or curr else l or r
        return lca(root)
Leave a Comment