Skip to content

Commit 95f6b08

Browse files
committed
5 not solved...BST 문제는 GPT 사용
1 parent 7e78f2a commit 95f6b08

File tree

1 file changed

+22
-5
lines changed

1 file changed

+22
-5
lines changed
Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,35 @@
11
"""
22
Blind 75 - LeetCode Problem 98: Validate Binary Search Tree
33
https://leetcode.com/problems/validate-binary-search-tree/
4-
이진 탐색 트리인지 확인하기
54
5+
중위 순회 - 왼쪽 서브트리 -> 루트 -> 오른쪽 서브트리
6+
중위 순회한 값이 오름차순인지 확인하면 된다.
67
"""
7-
from typing import Optional
8+
from typing import Optional, List
89

910
# Definition for a binary tree node.
1011
class TreeNode:
1112
def __init__(self, val=0, left=None, right=None):
1213
self.val = val
1314
self.left = left
1415
self.right = right
15-
16-
class Solution:
16+
17+
class Solution: # root = [2,1,3] answer = True
18+
def traverse(self, node: Optional[TreeNode], inorder: List[int]) -> None:
19+
if not node:
20+
return
21+
self.traverse(node.left, inorder)
22+
inorder.append(node.val)
23+
self.traverse(node.right, inorder)
24+
25+
1726
def isValidBST(self, root: Optional[TreeNode]) -> bool:
18-
27+
inorder = []
28+
self.traverse(root, inorder)
29+
30+
for i in range(1, len(inorder)):
31+
if inorder[i] <= inorder[i-1]:
32+
return False
33+
34+
return True
35+

0 commit comments

Comments
 (0)