Skip to content

Commit 24726b8

Browse files
committed
solve 2
1 parent ce6a0f6 commit 24726b8

File tree

1 file changed

+22
-0
lines changed
  • lowest-common-ancestor-of-a-binary-search-tree

1 file changed

+22
-0
lines changed
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
'''
2+
시간 복잡도: O(h) (h = 트리 높이)
3+
공간 복잡도: O(1)
4+
'''
5+
# Definition for a binary tree node.
6+
class TreeNode:
7+
def __init__(self, x):
8+
self.val = x
9+
self.left = None
10+
self.right = None
11+
12+
class Solution:
13+
def lowestCommonAncestor(self, root: 'TreeNode', p: 'TreeNode', q: 'TreeNode') -> 'TreeNode':
14+
node = root
15+
16+
while node:
17+
if p.val < node.val and q.val < node.val:
18+
node = node.left
19+
elif node.val < p.val and node.val < q.val:
20+
node = node.right
21+
else:
22+
return node

0 commit comments

Comments
 (0)