DSA Tracker

Medium

Kth Largest Element in BST

A medium BST problem included in Love Babbar 450. Below: the roles whose interviews prioritise this topic, and how to practise it.

Topic
BST
Sheets
1
Core for
2 roles
Platform
GeeksforGeeks

The problem

Given the root of a binary search tree and an integer k, return the kth largest element in the tree (1-indexed). This can be efficiently found by traversing the tree in reverse inorder.

Example 1

Input
root=[3,1,4,null,2], k=1
Output
4
Why
Reverse inorder gives 4,3,2,1. The 1st largest is 4.

Example 2

Input
root=[5,3,6,2,4,null,null,1], k=3
Output
4
Why
Reverse inorder gives 6,5,4,3,2,1. The 3rd largest is 4.

Example 3

Input
root=[1,null,2], k=2
Output
1
Why
Reverse inorder gives 2,1. The 2nd largest is 1.

Constraints

  • The number of nodes is in the range [1, 104].
  • 0 <= Node.val <= 104
  • 1 <= k <= the number of nodes.

How to think about it

Updated 2026-09-09

Symmetry turns kth largest into kth smallest. Instead of standard inorder (left-root-right) which produces ascending order, reverse inorder (right-root-left) yields descending order. Counting down k as each node is visited in descending order pinpoints the kth largest element the moment k drops to zero.

Approaches, worst first

  1. Inorder collect and index from end

    time O(n) · space O(n)

    Flatten the entire tree via standard inorder traversal into a list of size n, then read the element at index `n - k`. Works, but requires two passes and linear auxiliary space.

  2. Reverse inorder with counter

    time O(h + k) · space O(h)

    Recursively traverse right subtree, decrement k when visiting root, and recurse into left subtree if k is still positive. Aborts search as soon as k reaches 0.

  3. Reverse Morris traversalWrite this one

    time O(n) · space O(1)

    Use reverse Morris threading (connecting inorder predecessor in reverse order) to walk nodes in descending sequence without recursion stack or explicit heap memory. Halts when the kth node is reached.

Where people lose marks · 2
  • Continuing recursion after finding the answer: without an early termination check or sentinel return, the traversal continues processing remaining left branches and can overwrite the result.
  • Passing k by value across recursive calls in languages that do not maintain reference semantics; decrementing a local copy leaves sibling subtrees unaware of progress.

The theory behind it

BST — the ground this problem stands on. All BST problems

What BST is

A binary search tree is a binary tree that enforces a strict ordering rule at every node. Every value stored in a node's left branch must be smaller than the node itself, and every value in its right branch must be larger. Because this rule holds true everywhere down the tree, searching for a value does not require checking every branch. At each step, a single comparison lets you discard an entire half of the remaining nodes.

When to reach for it

Reach for a binary search tree when problems mention sorted tree structures, finding the kth smallest element, checking tree validity, or searching within a range of values. Signals include queries for the next greater element in a dynamic set, finding the lowest common ancestor in a sorted tree, or converting sorted arrays into height-balanced trees. When data must remain sorted while supporting continuous insertions and lookups, BST properties are the target.

How the pattern works

Use the ordering property to prune entire subtrees. When looking for a key, move left if the target is smaller than the current node, or move right if it is larger, stopping when values match or when reaching a null link. For validating a tree, do not merely compare a node to its immediate children; pass valid lower and upper value bounds down through recursive calls. Walking a valid binary search tree in-order visits all values in strictly increasing numerical order.

What each operation costs

OperationTime
search, insert, or delete on balanced treeO(log n)
search, insert, or delete on degenerate line treeO(n)
in-order traversal visiting all nodes in sorted orderO(n)
What usually goes wrong with BST
  • Validating a tree by checking only immediate children against the parent, missing violations where a node deep in the left subtree is larger than an ancestor higher up.
  • Allowing duplicate values on the wrong side when the problem specification requires strictly smaller values on the left and strictly greater on the right.
  • Deleting a node with two children by removing it directly instead of replacing its value with its in-order predecessor or successor and deleting that leaf instead.

Which roles need this problem

BST is a core topic for these 2 roles — if you're targeting one of them, this problem is early in your path, not optional.

Secondary for 6 more roles, including Full-Stack Developer, Android Developer, iOS Developer.

Track this in your role's order

Pick your target role and all 370 problems — including this one — resequence to what that interview actually asks. Free.

Start free

More BST problems

Problem set and role mapping as of .