Chaturmind
LearnDSASystem DesignInterview PrepDevOpsEngineering GrowthBlog
Start learning
Chaturmind

Structured learning paths for engineers who want to go deep. Written by practitioners.

Learn

  • Java
  • DSA
  • System Design
  • Spring Boot
  • AI / ML
  • DevOps
  • Engineering Growth
  • Java Interview Prep

Company

  • About
  • Blog
  • Contact

Legal

  • Privacy Policy
  • Terms of Service

© 2026 Chaturmind. All rights reserved.

👥10,173visitors

Built for engineers who want to go deep.

← Trees & Graphs

Binary Trees

  • Tree Traversal (DFS & BFS)
  • Binary Search Tree Operations
  • Practice problems

    Invert Binary Tree
  • Validate Binary Search Tree
  • Binary Tree Level Order Traversal
  • Binary Tree Inorder Traversal
  • Maximum Depth of Binary Tree
  • Binary Tree Zigzag Level Order Traversal
  • Construct Binary Tree from Preorder and Inorder Traversal
  • Insert into a Binary Search Tree
  • Kth Smallest Element in a BST
  • Lowest Common Ancestor of a Binary Tree
  • Lowest Common Ancestor of a Binary Search Tree
  • Path Sum II
  • Diameter of Binary Tree
  • Implement Trie (Prefix Tree)

Graph Algorithms

  • Graph DFS & BFS
  • Topological Sort
  • Union-Find (Disjoint Sets)
  • Practice problems

    Redundant Connection
  • Accounts Merge
  • Number of Islands
  • Clone Graph
  • Course Schedule
  • Rotting Oranges
  • Word Ladder
  • Course Schedule II
  • Number of Provinces
Chaturmind
← Trees & Graphs

Binary Trees

  • Tree Traversal (DFS & BFS)
  • Binary Search Tree Operations
  • Practice problems

    Invert Binary Tree
  • Validate Binary Search Tree
  • Binary Tree Level Order Traversal
  • Binary Tree Inorder Traversal
  • Maximum Depth of Binary Tree
  • Binary Tree Zigzag Level Order Traversal
  • Construct Binary Tree from Preorder and Inorder Traversal
  • Insert into a Binary Search Tree
  • Kth Smallest Element in a BST
  • Lowest Common Ancestor of a Binary Tree
  • Lowest Common Ancestor of a Binary Search Tree
  • Path Sum II
  • Diameter of Binary Tree
  • Implement Trie (Prefix Tree)

Graph Algorithms

  • Graph DFS & BFS
  • Topological Sort
  • Union-Find (Disjoint Sets)
  • Practice problems

    Redundant Connection
  • Accounts Merge
  • Number of Islands
  • Clone Graph
  • Course Schedule
  • Rotting Oranges
  • Word Ladder
  • Course Schedule II
  • Number of Provinces
HomeLearnTrees & GraphsBinary Trees
MediumTrees

Kth Smallest Element in a BST

treebstdfsstack

Problem

Given the root of a binary search tree and an integer k, return the k-th smallest value among all the nodes (1-indexed: k = 1 is the minimum).

      5
     / \
    3   6
   / \           k = 3   →   4       (sorted values: 2, 3, 4, 5, 6)
  2   4
  • Assume 1 ≤ k ≤ the number of nodes.
  • The BST property (left < node < right) means an inorder traversal visits values in sorted order. The task is to exploit that, ideally stopping as soon as the k-th value is reached.
  • Follow-up: if the BST is modified often (inserts and deletes) and you need k-th smallest queries frequently, how would you change the node structure? One answer is to store subtree sizes in each node.

Examples

Example 1

Input: root = [3,1,4,null,2], k = 1

Output: 1

Explanation: Inorder traversal visits 1, 2, 3, 4 — the 1st smallest is 1.

Constraints

  • •The number of nodes is n
  • •1 <= k <= n
  • •0 <= Node.val <= 10^4

Hints

Hint 1

What traversal order of a BST visits nodes in ascending sorted order automatically?

Hint 2

You don't need to collect every value into a list first — you can stop as soon as you've visited the kth node.

Hint 3

An iterative inorder traversal (the same stack-based technique from Binary Tree Inorder Traversal) lets you stop early without the awkwardness of returning early from deep recursion.

Solutions

public int kthSmallestBruteForce(TreeNode root, int k) {
    List<Integer> values = new ArrayList<>();
    inorder(root, values);
    return values.get(k - 1);
}
private void inorder(TreeNode node, List<Integer> values) {
    if (node == null) return;
    inorder(node.left, values);
    values.add(node.val);
    inorder(node.right, values);
}

Time: O(n) · Space: O(n)

Advertisement

Previous · Practice problem

Insert into a Binary Search Tree

Next · Practice problem

Lowest Common Ancestor of a Binary Tree