File tree Expand file tree Collapse file tree 1 file changed +22
-5
lines changed
validate-binary-search-tree Expand file tree Collapse file tree 1 file changed +22
-5
lines changed Original file line number Diff line number Diff line change 11"""
22Blind 75 - LeetCode Problem 98: Validate Binary Search Tree
33https://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.
1011class 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+
You can’t perform that action at this time.
0 commit comments