일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
Tags
- iterator
- 파이썬
- Convert Sorted List to Binary Search Tree
- attribute
- shiba
- Python
- 43. Multiply Strings
- 315. Count of Smaller Numbers After Self
- 109. Convert Sorted List to Binary Search Tree
- Python Implementation
- 프로그래머스
- Class
- kaggle
- Substring with Concatenation of All Words
- data science
- Generator
- 715. Range Module
- 밴픽
- DWG
- 시바견
- Regular Expression
- LeetCode
- Decorator
- Python Code
- Protocol
- t1
- 컴퓨터의 구조
- 30. Substring with Concatenation of All Words
- concurrency
- 운영체제
Archives
- Today
- Total
Scribbling
LeetCode: 99. Recover Binary Search Tree 본문
1. 초안
코드가 뭔가 복잡하다.
코드도 줄일겸 메모리를 O(1)으로 풀어보자.
# 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 recoverTree(self, root: Optional[TreeNode]) -> None:
"""
Do not return anything, modify root in-place instead.
"""
self.arr = []
self.helper(root)
left, right = None, None
for i in range(len(self.arr)-1):
if self.arr[i][0] > self.arr[i+1][0]:
left = i
break
i = left + 1
min_val = pow(2, 31) + 1
while i <= len(self.arr) - 1 and self.arr[i][0] < self.arr[left][0]:
if self.arr[i][0] < min_val:
min_val = self.arr[i][0]
right = i
i += 1
left = self.arr[left][1]
right = self.arr[right][1]
temp = left.val
left.val = right.val
right.val = temp
def helper(self, cur):
if cur.left != None:
self.helper(cur.left)
self.arr.append([cur.val, cur])
if cur.right != None:
self.helper(cur.right)
2. 메모리를 O(1)으로 줄인 방법
# 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 recoverTree(self, root: Optional[TreeNode]) -> None:
"""
Do not return anything, modify root in-place instead.
"""
INF = pow(2, 31) + 1
# self.reg1: fisrt error value
# self.reg2: second error value
self.reg1, self.reg2 = -INF, INF
# found_flag: True if an error has been found
self.found_flag = False
# swap node1's & node2's value
self.node1, self.node2 = None, None
self.dfs(root)
temp = self.node1.val
self.node1.val = self.node2.val
self.node2.val = temp
def dfs(self, cur):
if cur.left != None:
self.dfs(cur.left)
val = cur.val
# if no error until now
if not self.found_flag:
# valid
if val > self.reg1:
self.reg1 = val
self.node1 = cur
# error
else:
self.found_flag = True
if self.found_flag:
if val < self.reg1:
if val < self.reg2:
self.reg2 = val
self.node2 = cur
else:
return
if cur.right != None:
self.dfs(cur.right)
'Computer Science > Coding Test' 카테고리의 다른 글
LeetCode: 88. Merge Sorted Array (0) | 2021.10.09 |
---|---|
LeetCode: 98. Validate Binary Search Tree (0) | 2021.10.09 |
LeetCode: 89. Gray Code (0) | 2021.10.08 |
LeetCode: 126. Word Ladder II (0) | 2021.10.06 |
LeetCode: 86. Partition List (0) | 2021.10.06 |