| 일 | 월 | 화 | 수 | 목 | 금 | 토 |
|---|---|---|---|---|---|---|
| 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
- Substring with Concatenation of All Words
- Python Implementation
- 715. Range Module
- 시바견
- Convert Sorted List to Binary Search Tree
- 밴픽
- Generator
- Python Code
- Regular Expression
- attribute
- Python
- 파이썬
- iterator
- 프로그래머스
- 30. Substring with Concatenation of All Words
- LeetCode
- shiba
- 운영체제
- Class
- Protocol
- concurrency
- 109. Convert Sorted List to Binary Search Tree
- data science
- Decorator
- 컴퓨터의 구조
- 43. Multiply Strings
- t1
- kaggle
- DWG
- 315. Count of Smaller Numbers After Self
Archives
- Today
- Total
Scribbling
LeetCode: 230. Kth Smallest Element in a BST 본문
Computer Science/Coding Test
LeetCode: 230. Kth Smallest Element in a BST
focalpoint 2021. 12. 14. 21:33BST이기 때문에 Inorder-Traverse하면 된다.
K번째 원소를 찾으면 순회를 중단한다. 때문에 O(K)이다.
# 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 kthSmallest(self, root: Optional[TreeNode], k: int) -> int:
self.ret = 0
self.k = k
self.traverse(root)
return self.ret
# returns whether traverse should be continued
def traverse(self, node):
if node.left:
if self.traverse(node.left):
return True
self.k -= 1
if self.k == 0:
self.ret = node.val
if node.right:
if self.traverse(node.right):
return True
return False'Computer Science > Coding Test' 카테고리의 다른 글
| LeetCode: 101. Symmetric Tree (0) | 2021.12.19 |
|---|---|
| LeetCode: 236. Lowest Common Ancestor of a Binary Tree (0) | 2021.12.15 |
| LeetCode: 968. Binary Tree Cameras (0) | 2021.12.13 |
| LeetCode: 4. Median of Two Sorted Arrays (0) | 2021.12.12 |
| LeetCode: 337. House Robber III (0) | 2021.12.12 |