Algorithm · Scale AI · Medium
Given the serialized root of a rooted N-ary tree and two distinct node ids p_id and q_id, return the id of their lowest common ancestor (LCA). Each node is encoded as (id, value, children). The id is unique across the entire tree, value is an integer and may be duplicated, and children is a list of child nodes in the same format. Treat the ids as stand-ins for object references: never identify a node by its value. The LCA is the deepest node whose subtree contains both p_id…
Checking your access…