Skip to content

Commit

Permalink
Sync LeetCode submission Runtime - 83 ms (73.46%), Memory - 22.5 MB (…
Browse files Browse the repository at this point in the history
…30.76%)
  • Loading branch information
jimit105 committed Dec 22, 2024
1 parent 7518a9a commit 37a1447
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 0 deletions.
40 changes: 40 additions & 0 deletions 1816-lowest-common-ancestor-of-a-binary-tree-iv/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<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>

<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>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2018/12/14/binarytree.png" />
<pre>
<strong>Input:</strong> root = [3,5,1,6,2,0,8,null,null,7,4], nodes = [4,7]
<strong>Output:</strong> 2
<strong>Explanation:</strong> The lowest common ancestor of nodes 4 and 7 is node 2.
</pre>

<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2018/12/14/binarytree.png" />
<pre>
<strong>Input:</strong> root = [3,5,1,6,2,0,8,null,null,7,4], nodes = [1]
<strong>Output:</strong> 1
<strong>Explanation:</strong> The lowest common ancestor of a single node is the node itself.

</pre>

<p><strong class="example">Example 3:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2018/12/14/binarytree.png" />
<pre>
<strong>Input:</strong> root = [3,5,1,6,2,0,8,null,null,7,4], nodes = [7,6,2,4]
<strong>Output:</strong> 5
<strong>Explanation:</strong> The lowest common ancestor of the nodes 7, 6, 2, and 4 is node 5.
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
<li>The number of nodes in the tree is in the range <code>[1, 10<sup>4</sup>]</code>.</li>
<li><code>-10<sup>9</sup> &lt;= Node.val &lt;= 10<sup>9</sup></code></li>
<li>All <code>Node.val</code> are <strong>unique</strong>.</li>
<li>All <code>nodes[i]</code> will exist in the tree.</li>
<li>All <code>nodes[i]</code> are distinct.</li>
</ul>
37 changes: 37 additions & 0 deletions 1816-lowest-common-ancestor-of-a-binary-tree-iv/solution.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# Time: O(n)
# Space: O(n + m), O(n) for the recursive call stack, O(m) for the set `seen`

# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None

class Solution:
def lowestCommonAncestor(self, root: 'TreeNode', nodes: 'List[TreeNode]') -> 'TreeNode':
seen = set(nodes)

def dfs(root):
if root in seen:
return root

if not root:
return None

left = dfs(root.left)
right = dfs(root.right)

if left and right:
return root

if left and not right:
return left

if right and not left:
return right

return None

return dfs(root)

0 comments on commit 37a1447

Please sign in to comment.