Advertisement

Kth Smallest Element in a BST - LeetCode 230 Solution

Kth Smallest Element in a BST - Complete Solution Guide

Kth Smallest Element in a BST is LeetCode problem 230, a Medium level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.

Problem Statement

Given the root of a binary search tree, and an integer k , return the k th smallest value ( 1-indexed ) of all the values of the nodes in the tree . Example 1: Input: root = [3,1,4,null,2], k = 1 Output: 1 Example 2: Input: root = [5,3,6,2,4,null,null,1], k = 3 Output: 3 Constraints: The number of nodes in the tree is n . 1 <= k <= n <= 10 4 0 <= Node.val <= 10 4 Follow up: If the BST is modified often (i.e., we can do insert and delete operations) and you need to find the kth smallest frequentl

Detailed Explanation

The problem asks us to find the k-th smallest element in a Binary Search Tree (BST). The values are 1-indexed, meaning the smallest element is the 1st smallest, the second smallest is the 2nd smallest, and so on. A BST has the property that for any node, all nodes in its left subtree have values smaller than the node's value, and all nodes in its right subtree have values greater than the node's value. The input is the root of the BST and an integer `k`. The output is the value of the k-th smallest node in the BST.

Solution Approach

The solution employs an iterative in-order traversal of the BST using a stack. We maintain a stack to simulate the recursive calls of a standard in-order traversal. We traverse as far left as possible from the current node, pushing each node onto the stack. Then, we pop a node from the stack (which represents the next smallest element) and decrement `k`. If `k` becomes 0, we've found the k-th smallest element, and we return its value. Otherwise, we move to the right child of the popped node and repeat the process.

Step-by-Step Algorithm

  1. Step 1: Initialize an empty stack and set the current node to the root of the BST.
  2. Step 2: While the current node is not null, push the current node onto the stack and move to its left child. This process continues until the leftmost node is reached.
  3. Step 3: If the stack is empty, the traversal is complete. Otherwise, pop a node from the stack.
  4. Step 4: Decrement `k`. If `k` is 0, return the value of the popped node (the k-th smallest element).
  5. Step 5: Set the current node to the right child of the popped node, and repeat steps 2-4.

Key Insights

  • Insight 1: An in-order traversal of a BST yields the nodes in ascending order (sorted order). This is the fundamental property we leverage.
  • Insight 2: Iterative in-order traversal using a stack avoids recursion, potentially saving space in certain scenarios.
  • Insight 3: We can stop the in-order traversal once we have found the k-th smallest element, avoiding processing the entire tree.

Complexity Analysis

Time Complexity: O(n)

Space Complexity: O(n)

Topics

This problem involves: Tree, Depth-First Search, Binary Search Tree, Binary Tree.

Companies

Asked at: Agoda, Cisco, Google, Oracle.