Skip to content

Commit 83e9e75

Browse files
committed
Sync LeetCode submission Runtime - 47 ms (76.71%), Memory - 22.2 MB (6.06%)
1 parent 980ad1f commit 83e9e75

File tree

2 files changed

+78
-0
lines changed

2 files changed

+78
-0
lines changed
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<p>Given a binary tree, find the lowest common ancestor (LCA) of two given nodes in the tree.</p>
2+
3+
<p>According to the <a href="https://en.wikipedia.org/wiki/Lowest_common_ancestor" target="_blank">definition of LCA on Wikipedia</a>: &ldquo;The lowest common ancestor is defined between two nodes <code>p</code> and <code>q</code> as the lowest node in <code>T</code> that has both <code>p</code> and <code>q</code> as descendants (where we allow <b>a node to be a descendant of itself</b>).&rdquo;</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" style="width: 200px; height: 190px;" />
8+
<pre>
9+
<strong>Input:</strong> root = [3,5,1,6,2,0,8,null,null,7,4], p = 5, q = 1
10+
<strong>Output:</strong> 3
11+
<strong>Explanation:</strong> The LCA of nodes 5 and 1 is 3.
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" style="width: 200px; height: 190px;" />
16+
<pre>
17+
<strong>Input:</strong> root = [3,5,1,6,2,0,8,null,null,7,4], p = 5, q = 4
18+
<strong>Output:</strong> 5
19+
<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.
20+
</pre>
21+
22+
<p><strong class="example">Example 3:</strong></p>
23+
24+
<pre>
25+
<strong>Input:</strong> root = [1,2], p = 1, q = 2
26+
<strong>Output:</strong> 1
27+
</pre>
28+
29+
<p>&nbsp;</p>
30+
<p><strong>Constraints:</strong></p>
31+
32+
<ul>
33+
<li>The number of nodes in the tree is in the range <code>[2, 10<sup>5</sup>]</code>.</li>
34+
<li><code>-10<sup>9</sup> &lt;= Node.val &lt;= 10<sup>9</sup></code></li>
35+
<li>All <code>Node.val</code> are <strong>unique</strong>.</li>
36+
<li><code>p != q</code></li>
37+
<li><code>p</code> and <code>q</code> will exist in the tree.</li>
38+
</ul>
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# Approach 1: Recursive
2+
3+
# Time: O(n)
4+
# Space: O(n)
5+
6+
# Definition for a binary tree node.
7+
# class TreeNode:
8+
# def __init__(self, x):
9+
# self.val = x
10+
# self.left = None
11+
# self.right = None
12+
13+
class Solution:
14+
15+
def __init__(self):
16+
self.ans = None
17+
18+
def lowestCommonAncestor(self, root: 'TreeNode', p: 'TreeNode', q: 'TreeNode') -> 'TreeNode':
19+
20+
def recurse_tree(curr_node) -> bool:
21+
# If reached end of branch, return False
22+
if not curr_node:
23+
return False
24+
25+
left = recurse_tree(curr_node.left)
26+
right = recurse_tree(curr_node.right)
27+
28+
# If the current node is one of p or q
29+
mid = curr_node == p or curr_node == q
30+
31+
# If any two of the three flags (left, right, mid) become True
32+
if mid + left + right >= 2:
33+
self.ans = curr_node
34+
35+
# Return true if either of the three bool values is True
36+
return mid or left or right
37+
38+
recurse_tree(root)
39+
return self.ans
40+

0 commit comments

Comments
 (0)