230. Kth Smallest Element in a BST
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.
class Solution: def kthSmallest(self, root: Optional[TreeNode], k: int) -> int: s = [] ini = root ptr = 0 while ini or s : while ini : s.append(ini) ini = ini.left ini = s.pop() ptr += 1 if ptr == k: return ini.val ini = ini.right
Explaination :
Comments
Post a Comment