Quick Overview

Given the root of a binary search tree, return the smallest absolute difference between the values of any two different nodes. The problem tests tree traversal, use of the BST ordering property, and linear-time reasoning for trees with up to ten thousand nodes.

Find the smallest difference between any two node values in a binary search tree

Company: HashiCorp

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: easy

Interview Round: Other

Given the `root` of a binary search tree (BST), return the minimum absolute difference between the values of any two different nodes in the tree. ### Function Signature ```python def min_difference_in_bst(root: Optional[TreeNode]) -> int: ``` ### Rules - `TreeNode` has the fields `val`, `left` and `right`. - The examples write the tree as a level-order list in which `None` marks a missing child. The tree is built from that list before your function is called. - The input is a valid BST: every value in a node's left subtree is smaller than the node's value, and every value in its right subtree is larger. ### Constraints - The number of nodes in the tree is in the range `[2, 10^4]`. - `0 <= Node.val <= 10^5` ### Examples ```text Input: root = [4, 2, 6, 1, 3] Output: 1 ``` The tree has root 4 with children 2 and 6, and 2 has children 1 and 3. In sorted order the values are 1, 2, 3, 4, 6, and three neighboring pairs (1 and 2, 2 and 3, 3 and 4) differ by 1. ```text Input: root = [1, 0, 48, None, None, 12, 49] Output: 1 ``` The root 1 has children 0 and 48, and 48 has children 12 and 49. The sorted values 0, 1, 12, 48, 49 have gaps 1, 11, 36 and 1, so the minimum is 1 (between 0 and 1, and between 48 and 49). ```text Input: root = [20, 8, 30, None, 17] Output: 3 ``` The root 20 has children 8 and 30, and 17 is the right child of 8. The closest values are 17 and 20, which are not parent and child.

Overview: Given the root of a binary search tree, return the smallest absolute difference between the values of any two different nodes. The problem tests tree traversal, use of the BST ordering property, and linear-time reasoning for trees with up to ten thousand nodes.

Given the `root` of a binary search tree (BST), return the minimum absolute difference between the values of any two different nodes in the tree. ### How the tree is passed `root` is the tree written as a level-order list in which `None` marks a missing child. The first entry is the root value. The entries after it are read in pairs: each pair gives the left child and then the right child of the next present node, taking the present nodes in level order. `None` appears only for a missing child of a node that exists, and trailing `None` entries are left off, so the list has at most 2 * 10^4 - 1 entries. In JavaScript a missing child is `null`, in Java it is a `null` element of the list, and in C++ it is `std::nullopt`. You may rebuild the nodes (each with a value, a left child and a right child) or work with the list directly. The input is a valid BST: every value in a node's left subtree is smaller than the node's value, and every value in its right subtree is larger. So no two nodes share a value. ### Output Return one integer: the smallest value of |a - b| over all pairs of different nodes with values a and b. It is between 1 and 10^5, so it fits in a 32-bit signed integer (`int` in Java and C++). ### Constraints - The number of nodes in the tree is in the range `[2, 10^4]`. - `0 <= Node.val <= 10^5` - The tree is a valid BST as described above. ### Example 1 ```text Input: root = [4, 2, 6, 1, 3] Output: 1 ``` The root 4 has children 2 and 6, and 2 has children 1 and 3. In sorted order the values are 1, 2, 3, 4, 6, and three neighboring pairs (1 and 2, 2 and 3, 3 and 4) differ by 1. ### Example 2 ```text Input: root = [20, 8, 30, None, 17] Output: 3 ``` The root 20 has children 8 and 30, and 17 is the right child of 8. The closest values are 17 and 20, which are not parent and child.

Constraints

  • The number of nodes in the tree is in the range [2, 10^4].
  • 0 <= Node.val <= 10^5
  • The tree is a valid BST: every value in a node's left subtree is smaller than the node's value, and every value in its right subtree is larger, so all values are distinct.
  • root is the level-order list of the tree with None marking a missing child; None appears only for missing children of present nodes and trailing None entries are omitted, so the list has at most 2 * 10^4 - 1 entries.
  • The answer is between 1 and 10^5 and fits in a 32-bit signed integer.

Examples

Input: ([4, 2, 6, 1, 3],)

Expected Output: 1

Explanation: Source example 1: sorted values 1, 2, 3, 4, 6, and three neighboring pairs tie at 1.

Input: ([1, 0, 48, None, None, 12, 49],)

Expected Output: 1

Explanation: Source example 2: node 0 has two None children listed; sorted gaps 1, 11, 36, 1, so the tie at 1 includes the value 0.

Hints

  1. In Example 2 the closest values, 17 and 20, are not parent and child, so comparing each node only with its own children can miss the answer.
  2. Picture the node values written from smallest to largest. Which pairs in that list could hold the smallest difference?
  3. A tree with 10^4 nodes can be a single chain 10^4 levels deep, so plan for that depth.

Smallest Gap Between Two Values in a Binary Search Tree

easyHashiCorpSoftware Engineer

Given the root of a binary search tree (BST), return the minimum absolute difference between the values of any two different nodes in the tree.

How the tree is passed

root is the tree written as a level-order list in which None marks a missing child. The first entry is the root value. The entries after it are read in pairs: each pair gives the left child and then the right child of the next present node, taking the present nodes in level order. None appears only for a missing child of a node that exists, and trailing None entries are left off, so the list has at most 2 * 10^4 - 1 entries. In JavaScript a missing child is null, in Java it is a null element of the list, and in C++ it is std::nullopt. You may rebuild the nodes (each with a value, a left child and a right child) or work with the list directly.

The input is a valid BST: every value in a node's left subtree is smaller than the node's value, and every value in its right subtree is larger. So no two nodes share a value.

Output

Return one integer: the smallest value of |a - b| over all pairs of different nodes with values a and b. It is between 1 and 10^5, so it fits in a 32-bit signed integer (int in Java and C++).

Constraints

  • The number of nodes in the tree is in the range [2, 10^4].
  • 0 <= Node.val <= 10^5
  • The tree is a valid BST as described above.

Example 1

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

The root 4 has children 2 and 6, and 2 has children 1 and 3. In sorted order the values are 1, 2, 3, 4, 6, and three neighboring pairs (1 and 2, 2 and 3, 3 and 4) differ by 1.

Example 2

Input: root = [20, 8, 30, None, 17]
Output: 3

The root 20 has children 8 and 30, and 17 is the right child of 8. The closest values are 17 and 20, which are not parent and child.

Examples

Example 1
Input
([4, 2, 6, 1, 3],)
Output
1
Notes
Source example 1: sorted values 1, 2, 3, 4, 6, and three neighboring pairs tie at 1.
Example 2
Input
([1, 0, 48, None, None, 12, 49],)
Output
1
Notes
Source example 2: node 0 has two None children listed; sorted gaps 1, 11, 36, 1, so the tie at 1 includes the value 0.

Constraints

  • •The number of nodes in the tree is in the range [2, 10^4].
  • •0 <= Node.val <= 10^5
  • •The tree is a valid BST: every value in a node's left subtree is smaller than the node's value, and every value in its right subtree is larger, so all values are distinct.
  • •root is the level-order list of the tree with None marking a missing child; None appears only for missing children of present nodes and trailing None entries are omitted, so the list has at most 2 * 10^4 - 1 entries.
  • •The answer is between 1 and 10^5 and fits in a 32-bit signed integer.

Comments (0)

No comments yet. Be the first to comment!

Submit your answer

Approved first answer today earns 30 XP
Bold⌘BItalic⌘IInline code⌘`Bullet listTab to nest (○ ■)Numbered listTab to nest (a. i.)QuoteCode block
Formatting help
Signature
def min_difference_in_bst(root: list[int | None]) -> int:
Sample call
min_difference_in_bst([4, 2, 6, 1, 3])
Language
Case 1
Input
([4, 2, 6, 1, 3],)
Expected
1
Case 2
Input
([1, 0, 48, None, None, 12, 49],)
Expected
1

Browse More Questions

Show the approach

Approach

Approach: rebuild the child links from the level-order list, walk the tree in order (left subtree, node, right subtree) with an explicit stack, and keep the smallest difference between each visited value and the value visited just before it.

Building the tree: keep a queue of present nodes in level order, starting with the root. Each node taken from the queue claims the next two list entries as its left and right child, and every child that is not None joins the queue. This is the same rule the list was written with, so the rebuilt links match the original tree, and each entry is read once.

Why neighbors in sorted order are enough: an in-order walk of a valid BST visits the values in increasing order. For sorted values x1 < x2 < ... < xn and any i < j, xj - xi is the sum of the steps x(i+1) - xi, ..., xj - x(j-1), so it is at least the smallest single step. The minimum over all pairs therefore equals the minimum over consecutive in-order values. Those neighbors are often not parent and child: a node's in-order predecessor can be the rightmost node of its left subtree, several levels down, and its successor can be the leftmost node of its right subtree. Checking only tree edges, or only entries that sit next to each other in the list, can report a larger number.

Edge cases: a two-node tree has exactly one pair. A chain of 10^4 nodes is 10^4 levels deep, so the walk uses an explicit stack, since recursion that deep passes Python's default limit. None entries only mark missing children and are never read as values. Every value is at most 10^5, so all differences fit in a 32-bit int.

Time complexity:
O(n), where n is the length of the list (at most 2 * 10^4 - 1)
Space complexity:
O(n)