Skip to content

Commit

Permalink
Sync LeetCode submission Runtime - 52 ms (43.69%), Memory - 21.2 MB (…
Browse files Browse the repository at this point in the history
…5.73%)
  • Loading branch information
jimit105 committed Dec 22, 2024
1 parent 8eb36a8 commit 7518a9a
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 0 deletions.
49 changes: 49 additions & 0 deletions 1790-lowest-common-ancestor-of-a-binary-tree-iii/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<p>Given two nodes of a&nbsp;binary tree <code>p</code> and <code>q</code>, return <em>their&nbsp;lowest common ancestor (LCA)</em>.</p>

<p>Each node will have a reference to its parent node. The definition for <code>Node</code> is below:</p>

<pre>
class Node {
public int val;
public Node left;
public Node right;
public Node parent;
}
</pre>

<p>According to 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 two nodes p and q in a tree T is the lowest node that has both p and q as descendants (where we allow <b>a node to be a descendant of itself</b>).&quot;</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" style="width: 200px; height: 190px;" />
<pre>
<strong>Input:</strong> root = [3,5,1,6,2,0,8,null,null,7,4], p = 5, q = 1
<strong>Output:</strong> 3
<strong>Explanation:</strong> The LCA of nodes 5 and 1 is 3.
</pre>

<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2018/12/14/binarytree.png" style="width: 200px; height: 190px;" />
<pre>
<strong>Input:</strong> root = [3,5,1,6,2,0,8,null,null,7,4], p = 5, q = 4
<strong>Output:</strong> 5
<strong>Explanation:</strong> The LCA of nodes 5 and 4 is 5 since a node can be a descendant of itself according to the LCA definition.
</pre>

<p><strong class="example">Example 3:</strong></p>

<pre>
<strong>Input:</strong> root = [1,2], p = 1, q = 2
<strong>Output:</strong> 1
</pre>

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

<ul>
<li>The number of nodes in the tree is in the range <code>[2, 10<sup>5</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><code>p != q</code></li>
<li><code>p</code> and <code>q</code> exist in the tree.</li>
</ul>
29 changes: 29 additions & 0 deletions 1790-lowest-common-ancestor-of-a-binary-tree-iii/solution.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# Time: O(h), h = height of the tree
# Space: O(h)

"""
# Definition for a Node.
class Node:
def __init__(self, val):
self.val = val
self.left = None
self.right = None
self.parent = None
"""

class Solution:
def lowestCommonAncestor(self, p: 'Node', q: 'Node') -> 'Node':
visited = set()

while q:
visited.add(q.val)
q = q.parent

while p:
if p.val in visited:
return p
visited.add(p.val)
p = p.parent

return None

0 comments on commit 7518a9a

Please sign in to comment.