일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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
- attribute
- 109. Convert Sorted List to Binary Search Tree
- Python
- LeetCode
- Decorator
- Class
- 715. Range Module
- Protocol
- 운영체제
- concurrency
- Convert Sorted List to Binary Search Tree
- t1
- Generator
- Python Code
- Regular Expression
- 시바견
- 30. Substring with Concatenation of All Words
- shiba
- kaggle
- 밴픽
- DWG
- data science
- 파이썬
- iterator
- 컴퓨터의 구조
- 315. Count of Smaller Numbers After Self
- 프로그래머스
- 43. Multiply Strings
- Substring with Concatenation of All Words
- Python Implementation
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 |