# LC 1650. Lowest Common Ancestor of a Binary Tree III
# NOTE : there are also dict, recursive.. approaches
# V0''
# IDEA : set
class Solution:
def lowestCommonAncestor(self, p, q):
= set()
visited while p:
visited.add(p)= p.parent
p while q:
if q in visited:
return q
= q.parent q