Frontend School

Count Nodes in BST within Range

DSA
easy

Count Nodes in BST within Range

Easy
Tree
Binary Search Tree
DFS
Recursion

Overview

In data analytics and database systems, efficiently querying data within a specific range is a fundamental operation. Binary Search Trees (BSTs) are structured to facilitate such queries, allowing for faster retrieval compared to unsorted data. This problem challenges you to leverage the properties of a BST to count elements that fall within a given numerical range, simulating a common data filtering task.

What you need to do / Task

Your task is to implement a function countNodesInRange that takes the root of a Binary Search Tree (BST) and two integers, L and R, representing a closed numerical range [L, R]. The function should return the total count of nodes in the BST whose values are greater than or equal to L and less than or equal to R.

Input & output

Input:

  • root: The root node of a Binary Search Tree. Each TreeNode has an integer val and pointers left and right to its child nodes.
  • L: An integer representing the lower bound of the range (inclusive).
  • R: An integer representing the upper bound of the range (inclusive).

Output:

  • An integer representing the total count of nodes in the BST whose values are within the [L, R] range.

TreeNode Definition:

class TreeNode {
    val: number
    left: TreeNode | null
    right: TreeNode | null
    constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {
        this.val = (val===undefined ? 0 : val)
        this.left = (left===undefined ? null : left)
        this.right = (right===undefined ? null : right)
    }
}

Clarifications

  • The input root is guaranteed to be a valid Binary Search Tree.
  • The range [L, R] will always have L <= R.
  • If the tree is empty (root is null), the function should return 0.

Examples

1

Example 1

Input:

root = [10,5,15,3,7,null,18], L = 7, R = 15

Output:

3

Note:

Nodes with values 7, 10, and 15 are within the range [7, 15]. The left subtree of node 5 (containing node 3) is pruned because 3 < L. The right subtree of node 15 (containing node 18) is pruned because 18 > R.

2

Example 2

Input:

root = [10,5,15,3,7,null,18], L = 1, R = 5

Output:

2

Note:

Nodes with values 3 and 5 are within the range [1, 5]. Nodes 7, 10, 15, 18 are outside the range. The right subtree of node 5 (containing node 7) is pruned because 7 > R. The right subtree of node 10 (containing node 15 and 18) is pruned because 10 > R.

3

Example 3

Input:

root = [10,5,15], L = 20, R = 25

Output:

0

Note:

No nodes in the tree fall within the range [20, 25]. The search is efficiently pruned as the root (10) is less than L, directing search only to the right, and then 15 is also less than L, directing search to its right (which is null).

4

Example 4

Input:

root = [], L = 1, R = 10

Output:

0

Note:

The tree is empty, so there are no nodes to count.

Constraints

Constraints & assumptions

  • 0 <= Node.val <= 10^9
  • 0 <= L <= R <= 10^9
  • The number of nodes in the tree N is between 0 and 10^4.
  • The height of the BST can be up to N in the worst case (skewed tree).

What interviewers evaluate

  • Correctness and Efficiency: The solution should correctly count nodes within the specified range and leverage BST properties to avoid unnecessary traversals.
  • Algorithmic Design: Demonstrates a clear understanding of tree traversal and recursive/iterative approaches.
  • Edge Case Handling: Properly handles an empty tree or ranges outside the tree's values.
  • Code Quality: The code should be clean, readable, and well-structured.

Loading editor...