Class Solution
java.lang.Object
g0801_0900.s0865_smallest_subtree_with_all_the_deepest_nodes.Solution
865 - Smallest Subtree with all the Deepest Nodes.<p>Medium</p>
<p>Given the <code>root</code> of a binary tree, the depth of each node is <strong>the shortest distance to the root</strong>.</p>
<p>Return <em>the smallest subtree</em> such that it contains <strong>all the deepest nodes</strong> in the original tree.</p>
<p>A node is called <strong>the deepest</strong> if it has the largest depth possible among any node in the entire tree.</p>
<p>The <strong>subtree</strong> of a node is a tree consisting of that node, plus the set of all descendants of that node.</p>
<p><strong>Example 1:</strong></p>
<p><img src="https://s3-lc-upload.s3.amazonaws.com/uploads/2018/07/01/sketch1.png" alt="" /></p>
<p><strong>Input:</strong> root = [3,5,1,6,2,0,8,null,null,7,4]</p>
<p><strong>Output:</strong> [2,7,4]</p>
<p><strong>Explanation:</strong></p>
<p>We return the node with value 2, colored in yellow in the diagram.</p>
<p>The nodes coloured in blue are the deepest nodes of the tree.</p>
<p>Notice that nodes 5, 3 and 2 contain the deepest nodes in the tree but node 2 is the smallest subtree among them, so we return it.</p>
<p><strong>Example 2:</strong></p>
<p><strong>Input:</strong> root = [1]</p>
<p><strong>Output:</strong> [1]</p>
<p><strong>Explanation:</strong> The root is the deepest node in the tree.</p>
<p><strong>Example 3:</strong></p>
<p><strong>Input:</strong> root = [0,1,3,null,2]</p>
<p><strong>Output:</strong> [2]</p>
<p><strong>Explanation:</strong> The deepest node in the tree is 2, the valid subtrees are the subtrees of nodes 2, 1 and 0 but the subtree of node 2 is the smallest.</p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the tree will be in the range <code>[1, 500]</code>.</li>
<li><code>0 <= Node.val <= 500</code></li>
<li>The values of the nodes in the tree are <strong>unique</strong>.</li>
</ul>
<p><strong>Note:</strong> This question is the same as 1123: <a href="https://leetcode.com/problems/lowest-common-ancestor-of-deepest-leaves/" target="_top">https://leetcode.com/problems/lowest-common-ancestor-of-deepest-leaves/</a></p>
-
Constructor Summary
Constructors -
Method Summary
-
Constructor Details
-
Solution
public Solution()
-
-
Method Details
-
subtreeWithAllDeepest
-