Advertisement

Smallest Subtree with all the Deepest Nodes - LeetCode 865 Solution

Smallest Subtree with all the Deepest Nodes - Complete Solution Guide

Smallest Subtree with all the Deepest Nodes is LeetCode problem 865, a Medium level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.

Problem Statement

Given the root of a binary tree, the depth of each node is the shortest distance to the root . Return the smallest subtree such that it contains all the deepest nodes in the original tree. A node is called the deepest if it has the largest depth possible among any node in the entire tree. The subtree of a node is a tree consisting of that node, plus the set of all descendants of that node. Example 1: Input: root = [3,5,1,6,2,0,8,null,null,7,4] Output: [2,7,4] Explanation: We return the node with

Detailed Explanation

The problem asks us to find the smallest subtree of a given binary tree that contains all the deepest nodes. A deepest node is a node with the maximum depth in the tree (i.e., the longest path from the root to that node). The subtree of a node consists of the node itself and all its descendants. We need to return the root of the smallest such subtree. Smallest here means the subtree whose root is closest to the deepest nodes.

Solution Approach

The solution uses a recursive Depth-First Search (DFS) approach to traverse the binary tree. During the traversal, for each node, we calculate the depth of its left and right subtrees. Based on the depths, we determine the Lowest Common Ancestor (LCA) of the deepest nodes. If the depths of the left and right subtrees are equal, the current node is the LCA. If one subtree is deeper than the other, the LCA is the LCA of the deeper subtree.

Step-by-Step Algorithm

  1. Step 1: Define a recursive function `dfs(node)` that returns a tuple or object containing the depth of the subtree rooted at `node` and the LCA of the deepest nodes in that subtree.
  2. Step 2: Base case: If `node` is null, return a depth of 0 and a null LCA.
  3. Step 3: Recursively call `dfs` on the left and right children of `node` to obtain their respective depths and LCAs.
  4. Step 4: Compare the depths of the left and right subtrees.
  5. Step 5: If the left depth is greater than the right depth, the depth of the current subtree is `left_depth + 1`, and the LCA is the LCA of the left subtree.
  6. Step 6: If the right depth is greater than the left depth, the depth of the current subtree is `right_depth + 1`, and the LCA is the LCA of the right subtree.
  7. Step 7: If the left and right depths are equal, the depth of the current subtree is `left_depth + 1` (or `right_depth + 1`), and the LCA is the current node `node` itself.
  8. Step 8: Return the depth and LCA from the `dfs` function.
  9. Step 9: Call `dfs` on the root of the tree and return the LCA from the result.

Key Insights

  • Insight 1: We need to find the depth of the tree and the deepest nodes simultaneously.
  • Insight 2: A recursive Depth-First Search (DFS) is a suitable approach to traverse the tree and compute depths.
  • Insight 3: The Lowest Common Ancestor (LCA) of the deepest nodes is the root of the smallest subtree containing all deepest nodes.

Complexity Analysis

Time Complexity: O(n)

Space Complexity: O(n)

Topics

This problem involves: Hash Table, Tree, Depth-First Search, Breadth-First Search, Binary Tree.

Companies

Asked at: Salesforce.