LeetCode 230. Kth Smallest Element in a BST
考点 | 难度 |
---|---|
BST | Medium |
题目
Given the root of a binary search tree, and an integer k, return the kth smallest value (1-indexed) of all the values of the nodes in the tree.
思路
Stack
答案
class Solution:
def kthSmallest(self, root, k):
stack = []
while True:
while root:
stack.append(root)
root = root.left
root = stack.pop()
k -= 1
if not k:
return root.val
root = root.right