Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit fa12edd

Browse files
committed
add 235-lowest-common-ancestor-of-a-binary-search-tree python
1 parent f8d5998 commit fa12edd

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# Definition for a binary tree node.
2+
# class TreeNode:
3+
# def __init__(self, x):
4+
# self.val = x
5+
# self.left = None
6+
# self.right = None
7+
8+
class Solution:
9+
def lowestCommonAncestor(self, root: 'TreeNode', p: 'TreeNode', q: 'TreeNode') -> 'TreeNode':
10+
cur = root
11+
while cur:
12+
if p.val > cur.val and q.val > cur.val:
13+
cur = cur.right
14+
elif p.val < cur.val and q.val < cur.val:
15+
cur = cur.left
16+
else:
17+
return cur

0 commit comments

Comments
 (0)