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

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.

HashiCorp logo
HashiCorp
Nov 24, 2026
easySoftware EngineerOtherCoding & Algorithms
0
0

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

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

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.

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).

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.