Skip to content

530. Minimum Absolute Difference in BST

Edited by Charles

Easy · Python · Binary Tree

Problem

Read the original problem on LeetCode.

The official page contains the problem statement, examples, and constraints.

Explanation, examples & other approaches

Core idea

Iteration:

Reference to 98. Validate Binary Search Tree for the core idea in an order traversal.

Original study notes follow, including intermediate snippets and earlier attempts.

Iteration:

Reference to 98. Validate Binary Search Tree for the core idea in an order traversal.

# Definition for a binary tree node.
# class TreeNode:
#     def __init__(self, val=0, left=None, right=None):
#         self.val = val
#         self.left = left
#         self.right = right
class Solution:
    def getMinimumDifference(self, root: Optional[TreeNode]) -> int:
        minDiff = float('inf')

        stack = []
        cur = root
        pre = None

        while cur or stack:
            if cur:
                stack.append(cur)
                cur = cur.left
            else:
                cur = stack.pop()
                if pre:
                    minDiff = min(minDiff, cur.val - pre.val)
                pre = cur
                cur = cur.right

        return minDiff

Recursion:

Still the basic idea of the inorder traversal but in the recursive function.

Study record
Field Value
Attempts 1
Mastery ❚
The Latest Attempt January 16, 2026

Python solution

# Definition for a binary tree node.
# class TreeNode:
#     def __init__(self, val=0, left=None, right=None):
#         self.val = val
#         self.left = left
#         self.right = right
class Solution:
    def __init__(self):
        self.minDiff = float('inf')
        self.pre = None

    def getMinimumDifference(self, root: Optional[TreeNode]) -> int:
        self.traverse(root)
        return self.minDiff

    def traverse(self, node):
        if not node:
            return

        self.traverse(node.left)

        if self.pre:
            self.minDiff = min(self.minDiff, node.val - self.pre.val)
        self.pre = node

        self.traverse(node.right)

Back to Day 16 Binary Tree VI · All Algorithm notes