-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Sync LeetCode submission Runtime - 52 ms (43.69%), Memory - 21.2 MB (…
…5.73%)
- Loading branch information
Showing
2 changed files
with
78 additions
and
0 deletions.
There are no files selected for viewing
49 changes: 49 additions & 0 deletions
49
1790-lowest-common-ancestor-of-a-binary-tree-iii/README.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
<p>Given two nodes of a binary tree <code>p</code> and <code>q</code>, return <em>their 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>: "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>)."</p> | ||
|
||
<p> </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> </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> <= Node.val <= 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
29
1790-lowest-common-ancestor-of-a-binary-tree-iii/solution.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|