Second max in BST
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
Introduction
Finding the second maximum in a binary search tree is easier than it first appears because BST ordering already tells you where the largest values live. The maximum value is the rightmost node, so the second maximum is either that node's parent or the maximum value inside the rightmost node's left subtree.
Use The BST Property
In a BST:
- larger values are always found by moving right
- smaller values are always found by moving left
That means the maximum value is the node reached by following right pointers until there are no more.
The second maximum depends on what that maximum node looks like.
Two Cases For The Answer
There are exactly two structural cases.
- The maximum node has a left subtree. Then the second maximum is the rightmost node of that left subtree.
- The maximum node has no left subtree. Then the second maximum is its parent.
That logic avoids traversing the whole tree.
Iterative Python Example
This runs in O(h) time, where h is the tree height, because it follows only one path down the tree.
Example Walkthrough
Consider this tree:
The maximum is 40. It has no left subtree, so the second maximum is its parent, 30.
Now consider:
The maximum is 40, but it has a left subtree. The rightmost node in that left subtree is 37, so the second maximum is 37.
Why Full Traversal Is Unnecessary
A beginner solution often performs an in-order traversal, stores all values, and picks the second-to-last one.
That works, but it uses more time and memory than needed if your only goal is the second-largest value. The BST property already narrows the search to the right spine and possibly one left subtree.
Recursive Version
If you prefer recursion, the same two-case logic still applies.
The iterative version is often easier to explain in interviews, but either is fine if the edge cases are handled correctly.
Duplicates Change The Definition
This topic is simplest when the BST stores unique keys. If duplicates are allowed, you need to decide whether "second max" means:
- the second-largest distinct value, or
- simply the second node in descending order
Those are not the same when the maximum value appears multiple times.
Common Pitfalls
The biggest mistake is assuming the second maximum is always the parent of the maximum node. That fails when the maximum node has a left subtree. Another is forgetting the minimum-size edge case; a one-node tree has no second maximum. Developers also often solve the problem with a full traversal even though the BST property gives a more direct O(h) approach.
Summary
- The maximum in a BST is the rightmost node.
- If that node has a left subtree, the second maximum is the rightmost node in that subtree.
- Otherwise, the second maximum is the parent of the maximum node.
- The problem can be solved in
O(h)time without traversing the whole tree. - Handle small trees and duplicate-key semantics explicitly.
Related reading
- Secret Santa - Generating 'valid' permutations
- Secret santa algorithm
- Secure algorithm for creating license keys?
- Seeding the Newton iteration for cube root efficiently
- Secondary-only nodes in mongodb Replica set
- Seeking algorithm to invert reverse? mirror? turn inside-out a DAG
- Segmented Sieve of Eratosthenes?
- Select 50 items from list at random

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.