Smallest Subtree with all the Deepest Nodes

medium tree dfs lca

Problem

Given the root of a binary tree, the depth of a node is its distance from the root. A node is deepest if it has the largest depth in the whole tree. Return the smallest subtree that contains all the deepest nodes — equivalently, the lowest common ancestor of every deepest node.

Inputroot = [3,5,1,6,2,0,8,null,null,7,4]
Output[2,7,4]
Deepest nodes are 7 and 4; their lowest common ancestor is node 2.

def subtree_with_all_deepest(root):
    def dfs(node):
        if not node:
            return (0, None)
        ld, lnode = dfs(node.left)
        rd, rnode = dfs(node.right)
        if ld == rd:
            return (ld + 1, node)
        return (ld + 1, lnode) if ld > rd else (rd + 1, rnode)
    return dfs(root)[1]
function subtreeWithAllDeepest(root) {
  function dfs(node) {
    if (!node) return [0, null];
    const [ld, lnode] = dfs(node.left);
    const [rd, rnode] = dfs(node.right);
    if (ld === rd) return [ld + 1, node];
    return ld > rd ? [ld + 1, lnode] : [rd + 1, rnode];
  }
  return dfs(root)[1];
}
TreeNode subtreeWithAllDeepest(TreeNode root) {
    return dfs(root).node;
}
private Result dfs(TreeNode node) {
    if (node == null) return new Result(0, null);
    Result l = dfs(node.left), r = dfs(node.right);
    if (l.depth == r.depth) return new Result(l.depth + 1, node);
    return l.depth > r.depth
        ? new Result(l.depth + 1, l.node)
        : new Result(r.depth + 1, r.node);
}
pair<int, TreeNode*> dfs(TreeNode* node) {
    if (!node) return {0, nullptr};
    auto [ld, ln] = dfs(node->left);
    auto [rd, rn] = dfs(node->right);
    if (ld == rd) return {ld + 1, node};
    return ld > rd ? make_pair(ld + 1, ln) : make_pair(rd + 1, rn);
}
TreeNode* subtreeWithAllDeepest(TreeNode* root) {
    return dfs(root).second;
}
Time: O(n) Space: O(h)