530. Minimum Absolute Difference in BST

Minimum Absolute Difference in BST (LeetCode 530) is a Easy Data Structures and Algorithms interview problem you can solve, run and submit on Thita.ai. It belongs to the Tree Traversal Patterns (DFS & BFS) pattern, in the Recursive Inorder Traversal subpattern.

How to solve Minimum Absolute Difference in BST

Minimum Absolute Difference in BST is a Inorder Traversal problem, part of Tree Traversal Patterns (DFS & BFS). Master inorder traversal for BST validation and kth element problems. Produces sorted order in BST.

Work towards the solution in these steps:

  1. Recall that a Binary Search Tree (BST) has an in-order traversal that produces values in ascending order.
  2. Consider performing an in-order traversal of the BST to obtain the node values in sorted order.
  3. Think about how the minimum absolute difference between any two nodes relates to the differences between consecutive values in this sorted sequence.

The reference solution runs in O(n) time and O(h) space.

The Minimum Absolute Difference in BST solution and editorial walks through the full approach with reference code.

Problem statement

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.

Example 1:

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

Example 2:

Input: root = [1,0,48,null,null,12,49] Output: 1

Constraints:

- The number of nodes in the tree is in the range [2, 104].

- 0 <= Node.val <= 105

Note: This question is the same as 783: https://leetcode.com/problems/minimum-distance-between-bst-nodes/

Topics and companies

Topics: Tree, Depth-First Search, Breadth-First Search, Binary Search Tree, Binary Tree.

Reported in interviews at Google.

How to practise Minimum Absolute Difference in BST on Thita.ai

Starter code is provided in C, C#, C++, Go, Java, JavaScript and Python. Your solution runs against 5 test cases for this problem, with the AI coach available for a hint when you are stuck rather than a finished answer. The reference solution runs in O(n) time and O(h) space.

Open Minimum Absolute Difference in BST in the code editor, or read the Minimum Absolute Difference in BST editorial for a worked solution with its approach and complexity analysis.

Where Minimum Absolute Difference in BST sits in the DSA pattern sheet

Problems related to Minimum Absolute Difference in BST

Other problems that use the same Recursive Inorder Traversal and Tree Traversal Patterns (DFS & BFS) techniques:

Minimum Absolute Difference in BST FAQ

What is the LeetCode number of Minimum Absolute Difference in BST?

Minimum Absolute Difference in BST is LeetCode problem 530, rated Easy.

What pattern is used to solve Minimum Absolute Difference in BST?

Minimum Absolute Difference in BST is a Inorder Traversal problem. It belongs to the Tree Traversal Patterns (DFS & BFS) pattern, in the Recursive Inorder Traversal subpattern.

What is the time complexity of Minimum Absolute Difference in BST?

The reference solution runs in O(n) time and O(h) space.

Which companies ask Minimum Absolute Difference in BST?

Minimum Absolute Difference in BST has been reported in interviews at Google.

Preparing this workspace.