dfs(node)
that returns a tuple (max_depth, subtree)
where max_depth
is the maximum depth in the subtree rooted at node
, and subtree
is the smallest subtree containing all the deepest nodes.node
is None
, return (0, None)
.dfs
on the left and right children of node
to get their maximum depths and subtrees.node
is the LCA, and we return (max_depth + 1, node)
.dfs(root)
, and the second element of the returned tuple is the desired smallest subtree.