Skip to content

Commit 47bf27d

Browse files
committed
Sync LeetCode submission Runtime - 56 ms (55.67%), Memory - 21.1 MB (12.09%)
1 parent 52e5ea4 commit 47bf27d

File tree

1 file changed

+7
-10
lines changed
  • 0235-lowest-common-ancestor-of-a-binary-search-tree

1 file changed

+7
-10
lines changed
Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
# Approach 1 - Recursive Approach
1+
# Approach 1: Recursive
22

3-
# Time: O(N)
4-
# Space: O(N)
3+
# Time: O(n)
4+
# Space: O(n)
55

66
# Definition for a binary tree node.
77
# class TreeNode:
@@ -13,18 +13,15 @@
1313
class Solution:
1414
def lowestCommonAncestor(self, root: 'TreeNode', p: 'TreeNode', q: 'TreeNode') -> 'TreeNode':
1515
parent_val = root.val
16-
1716
p_val = p.val
1817
q_val = q.val
19-
20-
# if both values are greater than root value, search in right subtree
18+
19+
# If both p and q are greater than parent
2120
if p_val > parent_val and q_val > parent_val:
2221
return self.lowestCommonAncestor(root.right, p, q)
23-
24-
# if both values are lesser than root value, search in left subtree
22+
# If both p and q are lesser than parent
2523
elif p_val < parent_val and q_val < parent_val:
2624
return self.lowestCommonAncestor(root.left, p, q)
27-
25+
# We have found the split point, i.e. the LCA node.
2826
else:
2927
return root
30-

0 commit comments

Comments
 (0)