Untitled

 avatar
unknown
plain_text
a year ago
539 B
2
Indexable
class Solution(object):
    def kthSmallest(self, root, k):
        """
        :type root: TreeNode
        :type k: int
        :rtype: int
        """
        self.ans = root.val
        self.k = k
        def inorder(node):
            if node:
                inorder(node.left)
                if self.k > 0:
                    self.k -= 1
                    if self.k == 0:
                        self.ans = node.val
                        return
                inorder(node.right)
        inorder(root)
        return self.ans
Editor is loading...
Leave a Comment