일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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
- concurrency
- Convert Sorted List to Binary Search Tree
- shiba
- 밴픽
- 30. Substring with Concatenation of All Words
- attribute
- DWG
- 315. Count of Smaller Numbers After Self
- Python
- 프로그래머스
- Python Code
- 컴퓨터의 구조
- Decorator
- Regular Expression
- Generator
- 109. Convert Sorted List to Binary Search Tree
- LeetCode
- iterator
- 715. Range Module
- Class
- 운영체제
- Substring with Concatenation of All Words
- t1
- 43. Multiply Strings
- data science
- kaggle
- Protocol
- 시바견
- 파이썬
- Python Implementation
Archives
- Today
- Total
Scribbling
LeetCode: 173. Binary Search Tree Iterator 본문
Computer Science/Algorithms & Data Structures
LeetCode: 173. Binary Search Tree Iterator
focalpoint 2023. 9. 30. 02:43
Python Code with yield
# 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 BSTIterator:
def __init__(self, root: Optional[TreeNode]):
self.root = root
def generator(node):
if node.left is not None:
yield from generator(node.left)
yield node.val
if node.right is not None:
yield from generator(node.right)
self.generator = generator(self.root)
self.nxt = next(self.generator, None)
def next(self) -> int:
ret = self.nxt
self.nxt = next(self.generator, None)
return ret
def hasNext(self) -> bool:
return self.nxt is not None
# Your BSTIterator object will be instantiated and called as such:
# obj = BSTIterator(root)
# param_1 = obj.next()
# param_2 = obj.hasNext()
'Computer Science > Algorithms & Data Structures' 카테고리의 다른 글
[Programmers] N으로 표현 (1) | 2024.06.13 |
---|---|
Design HashMap -- Python Implementation (0) | 2023.10.05 |
Dynamic Programming: Finding The Recurrence Relation (0) | 2023.08.22 |
Number of pairs that satisfies the range condition (lower <= p[i] - p[j] <= upper) (0) | 2023.05.15 |
LeetCode: 1639. Number of Ways to Form a Target String Given a Dictionary (0) | 2023.04.07 |