Find the kth largest value in a binary search tree

Quick Overview

Find the kth largest value in a binary search tree, given its root and a valid rank k. The problem tests whether you can use the ordering property of a BST to stop as soon as the answer is known, and whether you can state the time and space cost in terms of the tree's height.

Find the kth largest value in a binary search tree

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Other

Given the `root` of a binary search tree and an integer `k`, return the `k`th largest value in the tree. It is guaranteed that `k` is valid, that is, `1 <= k <= n` where `n` is the number of nodes in the tree. As a follow-up, try to find the answer without traversing the entire tree. ### Function Signature ```python def kth_largest(root: Optional[TreeNode], k: int) -> int: ``` ### Rules - The function receives the root `TreeNode` of the tree. Each node has `val`, `left` and `right` attributes. - The examples write the tree as a level-order list, where `None` marks a missing child. - The tree is a valid binary search tree: for every node, all values in its left subtree are smaller than the node's value and all values in its right subtree are larger. - Ranks count from the top: `k = 1` is the largest value in the tree and `k = n` is the smallest. ### Constraints - The number of nodes in the tree is `n`. - `1 <= k <= n <= 10^4` - `0 <= Node.val <= 10^4` ### Examples ```text Input: root = [5, 3, 6, 2, 4, None, None, 1], k = 2 Output: 5 ``` The root is 5 with children 3 and 6, the node 3 has children 2 and 4, and 1 is the left child of 2. In descending order the values are 6, 5, 4, 3, 2, 1, so the 2nd largest is 5. ```text Input: root = [3, 1, 4, None, 2], k = 1 Output: 4 ``` Here 2 is the right child of 1. The largest value is 4, the right child of the root. ```text Input: root = [8, 4, 10, None, 6], k = 4 Output: 4 ``` The tree has 4 nodes and 6 is the right child of 4. With `k` equal to the node count, the answer is the smallest value: the descending order is 10, 8, 6, 4.

Overview: Find the kth largest value in a binary search tree, given its root and a valid rank k. The problem tests whether you can use the ordering property of a BST to stop as soon as the answer is known, and whether you can state the time and space cost in terms of the tree's height.

General logo
General
Nov 25, 2026
mediumSoftware EngineerOtherCoding & Algorithms
0
0

Given the root of a binary search tree and an integer k, return the kth largest value in the tree.

It is guaranteed that k is valid, that is, 1 <= k <= n where n is the number of nodes in the tree.

As a follow-up, try to find the answer without traversing the entire tree.

Function Signature

def kth_largest(root: Optional[TreeNode], k: int) -> int:

Rules

  • The function receives the root TreeNode of the tree. Each node has val , left and right attributes.
  • The examples write the tree as a level-order list, where None marks a missing child.
  • The tree is a valid binary search tree: for every node, all values in its left subtree are smaller than the node's value and all values in its right subtree are larger.
  • Ranks count from the top: k = 1 is the largest value in the tree and k = n is the smallest.

Constraints

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

Examples

Input: root = [5, 3, 6, 2, 4, None, None, 1], k = 2
Output: 5

The root is 5 with children 3 and 6, the node 3 has children 2 and 4, and 1 is the left child of 2. In descending order the values are 6, 5, 4, 3, 2, 1, so the 2nd largest is 5.

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

Here 2 is the right child of 1. The largest value is 4, the right child of the root.

Input: root = [8, 4, 10, None, 6], k = 4
Output: 4

The tree has 4 nodes and 6 is the right child of 4. With k equal to the node count, the answer is the smallest value: the descending order is 10, 8, 6, 4.