Minimum Absolute Difference in BST
Minimum Absolute Difference in BST 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.
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
- Tree Traversal Patterns (DFS & BFS) pattern guide
- Recursive Inorder Traversal subpattern
- The full DSA patterns sheet
- All coding practice problems
Problems related to Minimum Absolute Difference in BST
Other problems that use the same Recursive Inorder Traversal and Tree Traversal Patterns (DFS & BFS) techniques:
- Binary Search Tree Iterator — Medium, same subpattern
- Binary Tree Inorder Traversal — Easy, same subpattern
- Find Mode in Binary Search Tree — Easy, same subpattern
- Kth Smallest Element in a BST — Medium, same subpattern
- Validate Binary Search Tree — Medium, same subpattern
- All Nodes Distance K in Binary Tree — Medium, same pattern
Preparing your workspace
Progress is saved as you study.