Skip to content

Commit 37a1447

Browse files
committed
Sync LeetCode submission Runtime - 83 ms (73.46%), Memory - 22.5 MB (30.76%)
1 parent 7518a9a commit 37a1447

File tree

2 files changed

+77
-0
lines changed

2 files changed

+77
-0
lines changed
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<p>Given the <code>root</code> of a binary tree and an array of <code>TreeNode</code> objects <code>nodes</code>, return <em>the lowest common ancestor (LCA) of <strong>all the nodes</strong> in </em><code>nodes</code>. All the nodes will exist in the tree, and all values of the tree&#39;s nodes are <strong>unique</strong>.</p>
2+
3+
<p>Extending the <strong><a href="https://en.wikipedia.org/wiki/Lowest_common_ancestor" target="_blank">definition of LCA on Wikipedia</a></strong>: &quot;The lowest common ancestor of <code>n</code> nodes <code>p<sub>1</sub></code>, <code>p<sub>2</sub></code>, ..., <code>p<sub>n</sub></code> in a binary tree <code>T</code> is the lowest node that has every <code>p<sub>i</sub></code> as a <strong>descendant</strong> (where we allow <b>a node to be a descendant of itself</b>) for every valid <code>i</code>&quot;. A <strong>descendant</strong> of a node <code>x</code> is a node <code>y</code> that is on the path from node <code>x</code> to some leaf node.</p>
4+
5+
<p>&nbsp;</p>
6+
<p><strong class="example">Example 1:</strong></p>
7+
<img alt="" src="https://assets.leetcode.com/uploads/2018/12/14/binarytree.png" />
8+
<pre>
9+
<strong>Input:</strong> root = [3,5,1,6,2,0,8,null,null,7,4], nodes = [4,7]
10+
<strong>Output:</strong> 2
11+
<strong>Explanation:</strong> The lowest common ancestor of nodes 4 and 7 is node 2.
12+
</pre>
13+
14+
<p><strong class="example">Example 2:</strong></p>
15+
<img alt="" src="https://assets.leetcode.com/uploads/2018/12/14/binarytree.png" />
16+
<pre>
17+
<strong>Input:</strong> root = [3,5,1,6,2,0,8,null,null,7,4], nodes = [1]
18+
<strong>Output:</strong> 1
19+
<strong>Explanation:</strong> The lowest common ancestor of a single node is the node itself.
20+
21+
</pre>
22+
23+
<p><strong class="example">Example 3:</strong></p>
24+
<img alt="" src="https://assets.leetcode.com/uploads/2018/12/14/binarytree.png" />
25+
<pre>
26+
<strong>Input:</strong> root = [3,5,1,6,2,0,8,null,null,7,4], nodes = [7,6,2,4]
27+
<strong>Output:</strong> 5
28+
<strong>Explanation:</strong> The lowest common ancestor of the nodes 7, 6, 2, and 4 is node 5.
29+
</pre>
30+
31+
<p>&nbsp;</p>
32+
<p><strong>Constraints:</strong></p>
33+
34+
<ul>
35+
<li>The number of nodes in the tree is in the range <code>[1, 10<sup>4</sup>]</code>.</li>
36+
<li><code>-10<sup>9</sup> &lt;= Node.val &lt;= 10<sup>9</sup></code></li>
37+
<li>All <code>Node.val</code> are <strong>unique</strong>.</li>
38+
<li>All <code>nodes[i]</code> will exist in the tree.</li>
39+
<li>All <code>nodes[i]</code> are distinct.</li>
40+
</ul>
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# Time: O(n)
2+
# Space: O(n + m), O(n) for the recursive call stack, O(m) for the set `seen`
3+
4+
# Definition for a binary tree node.
5+
# class TreeNode:
6+
# def __init__(self, x):
7+
# self.val = x
8+
# self.left = None
9+
# self.right = None
10+
11+
class Solution:
12+
def lowestCommonAncestor(self, root: 'TreeNode', nodes: 'List[TreeNode]') -> 'TreeNode':
13+
seen = set(nodes)
14+
15+
def dfs(root):
16+
if root in seen:
17+
return root
18+
19+
if not root:
20+
return None
21+
22+
left = dfs(root.left)
23+
right = dfs(root.right)
24+
25+
if left and right:
26+
return root
27+
28+
if left and not right:
29+
return left
30+
31+
if right and not left:
32+
return right
33+
34+
return None
35+
36+
return dfs(root)
37+

0 commit comments

Comments
 (0)