1650. Lowest Common Ancestor of a Binary Tree III

Given two nodes of a binary tree p and q, return their lowest common ancestor (LCA). Each node will have a reference to its parent node.

This is essentially a linkedlist question instead of binary tree question.

def lowestCommonAncestor(self, p: 'Node', q: 'Node') -> 'Node':
    p1, p2 = p, q
    while p1 != p2:
        p1 = p1.parent if p1.parent else q
        p2 = p2.parent if p2.parent else p
        
    return p1