일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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
- 30. Substring with Concatenation of All Words
- LeetCode
- Regular Expression
- 315. Count of Smaller Numbers After Self
- 컴퓨터의 구조
- Python Implementation
- 프로그래머스
- Substring with Concatenation of All Words
- 운영체제
- 109. Convert Sorted List to Binary Search Tree
- DWG
- kaggle
- iterator
- Protocol
- Class
- t1
- Decorator
- Python Code
- attribute
- Python
- 밴픽
- Generator
- 43. Multiply Strings
- data science
- 시바견
- concurrency
- 715. Range Module
- Convert Sorted List to Binary Search Tree
- shiba
- 파이썬
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:43LeetCode - The World's Leading Online Programming Learning Platform
Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.
leetcode.com
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 |