|
| 1 | +<p>Given the <code>root</code> of a binary tree, return <em>the lowest common ancestor of its deepest leaves</em>.</p> |
| 2 | + |
| 3 | +<p>Recall that:</p> |
| 4 | + |
| 5 | +<ul> |
| 6 | + <li>The node of a binary tree is a leaf if and only if it has no children</li> |
| 7 | + <li>The depth of the root of the tree is <code>0</code>. if the depth of a node is <code>d</code>, the depth of each of its children is <code>d + 1</code>.</li> |
| 8 | + <li>The lowest common ancestor of a set <code>S</code> of nodes, is the node <code>A</code> with the largest depth such that every node in <code>S</code> is in the subtree with root <code>A</code>.</li> |
| 9 | +</ul> |
| 10 | + |
| 11 | +<p> </p> |
| 12 | +<p><strong class="example">Example 1:</strong></p> |
| 13 | +<img alt="" src="https://s3-lc-upload.s3.amazonaws.com/uploads/2018/07/01/sketch1.png" style="width: 600px; height: 510px;" /> |
| 14 | +<pre> |
| 15 | +<strong>Input:</strong> root = [3,5,1,6,2,0,8,null,null,7,4] |
| 16 | +<strong>Output:</strong> [2,7,4] |
| 17 | +<strong>Explanation:</strong> We return the node with value 2, colored in yellow in the diagram. |
| 18 | +The nodes coloured in blue are the deepest leaf-nodes of the tree. |
| 19 | +Note that nodes 6, 0, and 8 are also leaf nodes, but the depth of them is 2, but the depth of nodes 7 and 4 is 3.</pre> |
| 20 | + |
| 21 | +<p><strong class="example">Example 2:</strong></p> |
| 22 | + |
| 23 | +<pre> |
| 24 | +<strong>Input:</strong> root = [1] |
| 25 | +<strong>Output:</strong> [1] |
| 26 | +<strong>Explanation:</strong> The root is the deepest node in the tree, and it's the lca of itself. |
| 27 | +</pre> |
| 28 | + |
| 29 | +<p><strong class="example">Example 3:</strong></p> |
| 30 | + |
| 31 | +<pre> |
| 32 | +<strong>Input:</strong> root = [0,1,3,null,2] |
| 33 | +<strong>Output:</strong> [2] |
| 34 | +<strong>Explanation:</strong> The deepest leaf node in the tree is 2, the lca of one node is itself. |
| 35 | +</pre> |
| 36 | + |
| 37 | +<p> </p> |
| 38 | +<p><strong>Constraints:</strong></p> |
| 39 | + |
| 40 | +<ul> |
| 41 | + <li>The number of nodes in the tree will be in the range <code>[1, 1000]</code>.</li> |
| 42 | + <li><code>0 <= Node.val <= 1000</code></li> |
| 43 | + <li>The values of the nodes in the tree are <strong>unique</strong>.</li> |
| 44 | +</ul> |
| 45 | + |
| 46 | +<p> </p> |
| 47 | +<p><strong>Note:</strong> This question is the same as 865: <a href="https://leetcode.com/problems/smallest-subtree-with-all-the-deepest-nodes/" target="_blank">https://leetcode.com/problems/smallest-subtree-with-all-the-deepest-nodes/</a></p> |
0 commit comments