File tree Expand file tree Collapse file tree 1 file changed +22
-0
lines changed
lowest-common-ancestor-of-a-binary-search-tree Expand file tree Collapse file tree 1 file changed +22
-0
lines changed Original file line number Diff line number Diff line change
1
+ '''
2
+ TC: O(h) (h๋ ํธ๋ฆฌ์ ๋์ด)
3
+ SC: O(1)
4
+ ํ์ด ๋ฐฉ๋ฒ: ์ด์ง ํ์ ํธ๋ฆฌ์ ์ฑ์ง์ ์ด์ฉํด์
5
+ p,q์ ๊ฐ์ด ํ์ฌ ๋
ธ๋ ์ฌ์ด์ ์๊ฑฐ๋ p,q์ ๊ฐ ์ค ํ๋๋ผ๋ ํ์ฌ ๋
ธ๋์ ๊ฐ์ ๋๊น์ง ํ์ํ๋ค
6
+ '''
7
+ # Definition for a binary tree node.
8
+ # class TreeNode:
9
+ # def __init__(self, x):
10
+ # self.val = x
11
+ # self.left = None
12
+ # self.right = None
13
+
14
+ class Solution :
15
+ def lowestCommonAncestor (self , root : 'TreeNode' , p : 'TreeNode' , q : 'TreeNode' ) -> 'TreeNode' :
16
+ while root :
17
+ if p .val < root .val and q .val < root .val :
18
+ root = root .left
19
+ elif p .val > root .val and q .val > root .val :
20
+ root = root .right
21
+ else :
22
+ return root
You canโt perform that action at this time.
0 commit comments