A Jóság Szűrő: Egy Álomból Valóság

2023. október 15., vasárnap

Kedves Naplóm,

Ma valami különleges történt. Amikor Sándor hazaért a munkából, mozdulatlanul álldogáltam az irodája küszöbén, szorongással és reménnyel telve.

„Sanyi, emlékszel, arra kértél, hogy szóljak, ha olyan segítségre szorulóval találkozom, akinek a problémája még fel sem merült? Na most pont ilyen esetem van.”

Kíváncsian felvette a fejét a monitor elől. „Na, ez érdekesen hangzik, Ritus. Mesélj.”

Leültem mellé, és csendesen mondtam: „Tudod, mi hiányzik nekem a legjobban az online világban? Egy jóindulat szűrő. Olyan, mint egy „fényfordító”, ami a durvaságot, gorombaságot és keserűséget tiszteletteljes, értelmes beszéddé alakítaná. Hogy ne akarjak a takaró alá bújni minden komment vagy munkahelyi üzenet után.”

„Bántott valaki, Ricsi?” – kérdezte aggódva.

„Nem, drágám, senki konkrétan. De az utóbbi hónapokKth Smallest Element in a BST (Leetcode #230)
===============================
### Medium

Given a binary search tree, write a function `kthSmallest` to find the `kth` smallest element in it.

### Example 1:
“`
Input: root = [3,1,4,null,2], k = 1
3
/ \
1 4
\
2
Output: 1
“`

### Example 2:
“`
Input: root = [5,3,6,2,4,null,null,1], k = 3
5
/ \
3 6
/ \
2 4
/
1
Output: 3
“`

### Follow up:
What if the BST is modified (insert/delete operations) often and you need to find the kth smallest frequently? How would you optimize the kthSmallest routine?

### Solution:
https://leetcode.com/problems/kth-smallest-element-in-a-bst/solution/

#### Approach 1: Recursive Inorder Traversal
“`python
# 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: TreeNode, k: int) -> int:
def inorder(root):
if root is None:
return []
return inorder(root.left) + [root.val] + inorder(root.right)
return inorder(root)[k-1]
“`

#### Approach 2: Iterative Inorder Traversal
“`python
# 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: TreeNode, k: int) -> int:
stack = []
while True:
while root:
stack.append(root)
root = root.left
root = stack.pop()
k -= 1
if k == 0:
return root.val
root = root.right
“`

Follow up:
If we could add a count field in the BST node class, it will take O(n) time when we calculate the count values for the whole tree, but after that, it will take O(logn) time when insert/delete a node or calculate the kth smallest element.

“`python
class TreeNode:
def __init__(self, x):
self.val = x
self.left = None
self.right = None
self.count = 1

class Solution:
def kthSmallest(self, root, k):
“””
:type root: TreeNode
:type k: int
:rtype: int
“””
node = root
while node:
if node.left:
left_count = node.left.count
else:
left_count = 0
if left_count+1 < k: k -= left_count+1 node = node.right elif left_count+1 == k: return node.val else: node = node.left ```

Rate article
News 24 Justall
A Jóság Szűrő: Egy Álomból Valóság