# 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
T:O(n), S:O(n), inorder sorted
class Solution: def kthSmallest(self, root: TreeNode, k: int) -> int: ans = [] if not root: return def inorder(node): if not node or len(ans) == k: return inorder(node.left) if len(ans)<k: ans.append(node.val) inorder(node.right) else: return inorder(root) return ans[-1]