230. Kth Smallest Element in a BST
https://leetcode.com/problems/kth-smallest-element-in-a-bst
Python
class Solution:
def __init__(self):
self.rank = 0
self.result = 0
def kthSmallest(self, root: Optional[TreeNode], k: int) -> int:
self.rank = 0
self.result = 0
self._travel(root, k)
return self.result
def _travel(self, node, k):
if not node:
return self.rank
self._travel(node.left, k)
self.rank += 1
if self.rank == k:
self.result = node.val
return
self._travel(node.right, k)