Skip to content

Commit 53bca8d

Browse files
authored
Create Lowest_Common_Ancestor.py
1 parent 04b285e commit 53bca8d

File tree

1 file changed

+12
-0
lines changed

1 file changed

+12
-0
lines changed
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# Time: O(N)
2+
class Solution:
3+
def lowestCommonAncestor(self, root: 'TreeNode', p: 'TreeNode', q: 'TreeNode') -> 'TreeNode':
4+
def find_LCA(root, p, q):
5+
if root.val < p.val and root.val < q.val:
6+
return find_LCA(root.right, p, q)
7+
elif root.val > p.val and root.val > q.val:
8+
return find_LCA(root.left, p, q)
9+
else:
10+
return root
11+
12+
return find_LCA(root, p, q)

0 commit comments

Comments
 (0)