Your question is LCA with Parent Pointers. Start with the requirements on the right.
Run and submit as often as you like. When you're ready, talk me through your approach or go straight to the code.
Lowest Common Ancestor of a Binary Tree (LC 1650 variant where nodes have parent pointers), solve in Time O(depth) and Space O(1).
Asked in the Phone Screen stage. The tree is represented by a parent-pointer mapping for grading: parent[node] gives a node's parent, and the root has no entry or maps to null.
Implement lowest_common_ancestor(parent, p, q). Return the node identifier representing the lowest common ancestor of p and q. The nodes belong to the same binary tree.
def lowest_common_ancestor(parent, p, q):