File tree Expand file tree Collapse file tree
validate-binary-search-tree Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ # https://leetcode.com/problems/3sum
2+
3+ class Solution (object ):
4+ def threeSum (self , nums ):
5+ nums .sort ()
6+ result = []
7+
8+ for i in range (len (nums ) - 2 ):
9+
10+ if i > 0 and nums [i ] == nums [i - 1 ]:
11+ continue
12+
13+ left = i + 1
14+ right = len (nums ) - 1
15+
16+ while left < right :
17+ total = nums [i ] + nums [left ] + nums [right ]
18+
19+ if total < 0 :
20+ left += 1
21+
22+ elif total > 0 :
23+ right -= 1
24+
25+ else :
26+ result .append ([nums [i ], nums [left ], nums [right ]])
27+
28+ while left < right and nums [left ] == nums [left + 1 ]:
29+ left += 1
30+
31+ while left < right and nums [right ] == nums [right - 1 ]:
32+ right -= 1
33+
34+ left += 1
35+ right -= 1
36+
37+ return result
Original file line number Diff line number Diff line change 1+ # https://leetcode.com/problems/validate-binary-search-tree/description/
2+
3+ # λμ ν μνμ΄μ μ§νΌν°μ λμμ λ°μμ΅λλ€..TT
4+ # μλ‘ μκ² λ κ°λ
5+ # μ΄μ§ νΈλ¦¬λ νλμ λ
Έλκ° μ΅λ λ κ°μ μμ λ
Έλλ₯Ό κ°μ§λ νΈλ¦¬ μλ£κ΅¬μ‘°
6+ # λ κ°μ ν¬μΈν°λ₯Ό κ°μ§κ³ μμΌλ©°, 루νΈμμ μμν΄μ μλλ‘ λ΄λ €κ°λ κ³μΈ΅ ꡬ쑰
7+ # Binary Search Tree(BST)λ μ΄μ§ νΈλ¦¬μ ν μ’
λ₯λ‘ μ΄λ€ λ
Έλμ μΌμͺ½ μλΈνΈλ¦¬μ μλ λͺ¨λ κ°μ ν΄λΉ λ
Έλλ³΄λ€ μκ³ μ€λ₯Έμͺ½ μλΈνΈλ¦¬μ μλ λͺ¨λ κ°μ ν΄λΉ λ
Έλλ³΄λ€ νΌ
8+ # μ΄ λ¬Έμ λ μ£Όμ΄μ§ νΈλ¦¬κ° μ΄ κ·μΉμ λ§μ‘±νλμ§ νμΈνλ λ¬Έμ μ
9+ # κ° λ
Έλκ° κ°μ§ μ μλ νμ© λ²μ(min, max)λ₯Ό μ μ§νλ©΄μ κ²μ¬ν΄μΌ ν¨
10+ class Solution :
11+ def isValidBST (self , root ):
12+
13+ def dfs (node , low , high ):
14+ if not node :
15+ return True
16+
17+ if node .val <= low or node .val >= high :
18+ return False
19+
20+ return (
21+ dfs (node .left , low , node .val ) and
22+ dfs (node .right , node .val , high )
23+ )
24+
25+ return dfs (root , float ("-inf" ), float ("inf" ))
You canβt perform that action at this time.
0 commit comments