Skip to main content

Kth Smallest Element in a BST

LeetCode 230 | Difficulty: Medium​

Medium

Problem Description​

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 frequently, how would you optimize?

Topics: Tree, Depth-First Search, Binary Search Tree, Binary Tree


Approach​

Tree DFS​

Traverse the tree recursively (or with a stack). At each node, decide: what information do I need from the left/right subtrees? Process: go left β†’ go right β†’ combine results. Consider preorder, inorder, or postorder traversal based on when you need to process the node.

When to use

Path problems, subtree properties, tree structure manipulation.


Solutions​

Solution 1: C# (Best: 108 ms)​

MetricValue
Runtime108 ms
Memory28.9 MB
Date2021-09-23
Solution
/**
* Definition for a binary tree node.
* public class TreeNode {
* public int val;
* public TreeNode left;
* public TreeNode right;
* public TreeNode(int val=0, TreeNode left=null, TreeNode right=null) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
public class Solution {
public int KthSmallest(TreeNode root, int k) {
int count = k;
System.Collections.Generic.Stack<TreeNode> s = new Stack<TreeNode>();

TreeNode current = root;
s.Push(current);
while(s.Count != 0)
{
while(current != null)
{
s.Push(current);
current = current.left;
}
var popped = s.Pop();
if(--count == 0)
{
return popped.val;
}
current = popped.right;
}
return -1;
}
}
πŸ“œ 1 more C# submission(s)

Submission (2021-09-22) β€” 108 ms, 28.6 MB​

/**
* Definition for a binary tree node.
* public class TreeNode {
* public int val;
* public TreeNode left;
* public TreeNode right;
* public TreeNode(int val=0, TreeNode left=null, TreeNode right=null) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
public class Solution {
public int KthSmallest(TreeNode root, int k) {
int result = 0;
int count = k;
helper(root, ref result, ref count);
return result;

}

public void helper(TreeNode n, ref int result, ref int count)
{
if(n.left != null)
{
helper(n.left, ref result, ref count);
}
count--;
if(count==0)
{
result = n.val;
return;
}
if(n.right != null) helper(n.right, ref result, ref count);

}
}

Complexity Analysis​

ApproachTimeSpace
Tree Traversal$O(n)$$O(h)$

Interview Tips​

Key Points
  • Discuss the brute force approach first, then optimize. Explain your thought process.
  • Consider: "What information do I need from each subtree?" β€” this defines your recursive return value.
  • LeetCode provides 4 hint(s) for this problem β€” try solving without them first.
πŸ’‘ Hints

Hint 1: Try to utilize the property of a BST.

Hint 2: Try in-order traversal. (Credits to @chan13)

Hint 3: What if you could modify the BST node's structure?

Hint 4: The optimal runtime complexity is O(height of BST).