Skip to content

Commit 98ce841

Browse files
committed
Sync LeetCode submission Runtime - 0 ms (100.00%), Memory - 18.1 MB (71.52%)
1 parent d1a7565 commit 98ce841

File tree

2 files changed

+76
-0
lines changed

2 files changed

+76
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
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>&nbsp;</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&#39;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>&nbsp;</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 &lt;= Node.val &lt;= 1000</code></li>
43+
<li>The values of the nodes in the tree are <strong>unique</strong>.</li>
44+
</ul>
45+
46+
<p>&nbsp;</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>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# Approach 1: Recursion
2+
3+
# Time: O(n)
4+
# Space: O(n)
5+
6+
# Definition for a binary tree node.
7+
# class TreeNode:
8+
# def __init__(self, val=0, left=None, right=None):
9+
# self.val = val
10+
# self.left = left
11+
# self.right = right
12+
13+
class Solution:
14+
def lcaDeepestLeaves(self, root: Optional[TreeNode]) -> Optional[TreeNode]:
15+
def dfs(root):
16+
if not root:
17+
return 0, None
18+
19+
left = dfs(root.left)
20+
right = dfs(root.right)
21+
22+
if left[0] > right[0]:
23+
return left[0] + 1, left[1]
24+
if left[0] < right[0]:
25+
return right[0] + 1, right[1]
26+
return left[0] + 1, root
27+
28+
return dfs(root)[1]
29+

0 commit comments

Comments
 (0)