Skip to content

Commit 8eb36a8

Browse files
committed
Sync LeetCode submission Runtime - 152 ms (34.90%), Memory - 22.3 MB (6.21%)
1 parent 83e9e75 commit 8eb36a8

File tree

2 files changed

+83
-0
lines changed

2 files changed

+83
-0
lines changed
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<p>Given the <code>root</code> of a binary tree, return <em>the lowest common ancestor (LCA) of two given nodes, </em><code>p</code><em> and </em><code>q</code>. If either node <code>p</code> or <code>q</code> <strong>does not exist</strong> in the tree, return <code>null</code>. All values of the nodes in the tree are <strong>unique</strong>.</p>
2+
3+
<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 <code>p</code> and <code>q</code> in a binary tree <code>T</code> is the lowest node that has both <code>p</code> and <code>q</code> as <strong>descendants</strong> (where we allow <b>a node to be a descendant of itself</b>)&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], p = 5, q = 1
10+
<strong>Output:</strong> 3
11+
<strong>Explanation:</strong> The LCA of nodes 5 and 1 is 3.</pre>
12+
13+
<p><strong class="example">Example 2:</strong></p>
14+
15+
<p><img alt="" src="https://assets.leetcode.com/uploads/2018/12/14/binarytree.png" /></p>
16+
17+
<pre>
18+
<strong>Input:</strong> root = [3,5,1,6,2,0,8,null,null,7,4], p = 5, q = 4
19+
<strong>Output:</strong> 5
20+
<strong>Explanation:</strong> The LCA of nodes 5 and 4 is 5. A node can be a descendant of itself according to the definition of LCA.</pre>
21+
22+
<p><strong class="example">Example 3:</strong></p>
23+
24+
<p><img alt="" src="https://assets.leetcode.com/uploads/2018/12/14/binarytree.png" /></p>
25+
26+
<pre>
27+
<strong>Input:</strong> root = [3,5,1,6,2,0,8,null,null,7,4], p = 5, q = 10
28+
<strong>Output:</strong> null
29+
<strong>Explanation:</strong> Node 10 does not exist in the tree, so return null.
30+
</pre>
31+
32+
<p>&nbsp;</p>
33+
<p><strong>Constraints:</strong></p>
34+
35+
<ul>
36+
<li>The number of nodes in the tree is in the range <code>[1, 10<sup>4</sup>]</code>.</li>
37+
<li><code>-10<sup>9</sup> &lt;= Node.val &lt;= 10<sup>9</sup></code></li>
38+
<li>All <code>Node.val</code> are <strong>unique</strong>.</li>
39+
<li><code>p != q</code></li>
40+
</ul>
41+
42+
<p>&nbsp;</p>
43+
<strong>Follow up:</strong>&nbsp;Can you find the LCA traversing the tree, without checking nodes existence?
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# Approach: Recursive approach from Lowest Common Ancestor of a Binary Tree (https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-tree/)
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)