일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | |||
5 | 6 | 7 | 8 | 9 | 10 | 11 |
12 | 13 | 14 | 15 | 16 | 17 | 18 |
19 | 20 | 21 | 22 | 23 | 24 | 25 |
26 | 27 | 28 | 29 | 30 | 31 |
Tags
- Class
- Substring with Concatenation of All Words
- data science
- Python Code
- Protocol
- 밴픽
- shiba
- DWG
- iterator
- 프로그래머스
- 운영체제
- attribute
- 30. Substring with Concatenation of All Words
- kaggle
- 시바견
- concurrency
- Python
- t1
- Decorator
- Python Implementation
- 715. Range Module
- Convert Sorted List to Binary Search Tree
- 파이썬
- 43. Multiply Strings
- Regular Expression
- 315. Count of Smaller Numbers After Self
- LeetCode
- Generator
- 컴퓨터의 구조
- 109. Convert Sorted List to Binary Search Tree
Archives
- Today
- Total
Scribbling
LeetCode: 98. Validate Binary Search Tree 본문
Computer Science/Coding Test
LeetCode: 98. Validate Binary Search Tree
focalpoint 2021. 10. 9. 17:20Binary Search Tree의 Validation 문제이다.
BST 특성상 아래와 같이 순회하면 정렬된다는 특성을 이용한다.
즉, 순회하면서 점점 value가 커지는지만 체크하면 끝.
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
class Solution:
def isValidBST(self, root: Optional[TreeNode]) -> bool:
self.reg = -pow(2, 31) - 1
return self.dfs(root)
def dfs(self, node):
if node.left != None:
if not self.dfs(node.left):
return False
if node.val <= self.reg:
return False
else:
self.reg = node.val
if node.right != None:
if not self.dfs(node.right):
return False
return True
'Computer Science > Coding Test' 카테고리의 다른 글
LeetCode: 90. Subsets II (0) | 2021.10.10 |
---|---|
LeetCode: 88. Merge Sorted Array (0) | 2021.10.09 |
LeetCode: 99. Recover Binary Search Tree (0) | 2021.10.08 |
LeetCode: 89. Gray Code (0) | 2021.10.08 |
LeetCode: 126. Word Ladder II (0) | 2021.10.06 |