Kth Smallest Element in a BST

Binary Search Tree
Medium

Approach

Diagrams

Analysis

Time

O(n)

Space

O(n)
class Solution:
    def kthSmallest(self, root, k):
        nodeValsInorder = self.getNodeValsInorder(root)
        kthSmallestIdx = k - 1
        return nodeValsInorder[kthSmallestIdx]

    def getNodeValsInorder(self, root):
        nodeValsInorder = []
        self.inorderTraversal(root, nodeValsInorder)
        return nodeValsInorder

    def inorderTraversal(self, node, nodeValsInorder):
        if not node:
            return

        self.inorderTraversal(node.left, nodeValsInorder)
        nodeValsInorder.append(node.val)
        self.inorderTraversal(node.right, nodeValsInorder)

Learn

#Inorder

Videos