↓Skip to main content
  1. LeetCode/

LeetCode 783: Minimum Distance Between BST Nodes

·2 mins· ·
LeetCode Easy Tree Depth-First-Search Breadth-First-Search Binary-Search-Tree
Wei Yi Chung
Author
Wei Yi Chung
Working at the contributing of open source, distributed systems, and data engineering.
Table of Contents

Meta Data
#

Difficulty: easy First Attempt: 2022-09-06

  • Submissions: Runtime Error → Accepted ×3 → Wrong Answer ×2 → Runtime Error → Wrong Answer ×6 → Runtime Error → Wrong Answer ×4 → Accepted

Approach
#

A BST’s in-order traversal is sorted, and in a sorted sequence the closest pair is adjacent (Tree Template 3). Remember the previous value during the in-order walk and update the answer with each difference. This is the same problem as LC 530.

My Attempts
#

This took 19 submissions, nearly all from one misunderstanding: assuming the smallest difference is always between a parent and a child, so the recursion compared each node with its children (or with values returned from subtrees). The closest two values are adjacent in the in-order traversal, not necessarily parent and child: in [3, None, 8, 4], 3 and 4 are not parent and child and differ by 1; it returned 4. Several versions also subtracted from -inf / inf initial values and returned inf.

Solution
#

class Solution:
    def minDiffInBST(self, root: Optional[TreeNode]) -> int:
        best, prev = float("inf"), None
        stack, cur = [], root
        while cur or stack:
            while cur:
                stack.append(cur)
                cur = cur.left
            cur = stack.pop()
            if prev is not None:
                best = min(best, cur.val - prev)
            prev = cur.val
            cur = cur.right
        return best

Complexity
#

Time O(n), space O(h).

Mistakes to Avoid
#

  • The closest values need not be parent and child; compare in-order neighbors.

Interview Explanation
#

A BST’s in-order is sorted and the minimum difference lies between neighbors, so I keep the previous value during the in-order walk and update the minimum. Time O(n).

Related

LeetCode 530: Minimum Absolute Difference in BST
·1 min
LeetCode Easy Tree Depth-First-Search Breadth-First-Search Binary-Search-Tree
LeetCode 530 note: a BST’s in-order is ascending, so the minimum difference is between in-order neighbors; remember the previous value and compare
LeetCode 637: Average of Levels in Binary Tree
·1 min
LeetCode Easy Tree Depth-First-Search Breadth-First-Search Binary-Tree
LeetCode 637 note: level-order traversal, dividing each level’s sum by its count
LeetCode 559: Maximum Depth of N-ary Tree
·1 min
LeetCode Easy Tree Depth-First-Search Breadth-First-Search
LeetCode 559 note: depth is 1 plus the deepest child, or 1 with no children
LeetCode 501: Find Mode in Binary Search Tree
·2 mins
LeetCode Easy Tree Depth-First-Search Binary-Search-Tree Binary-Tree
LeetCode 501 note: a BST’s in-order is sorted, so equal values are contiguous; count the current run during in-order, resetting the answer on a new maximum and appending on a tie
LeetCode 404: Sum of Left Leaves
·1 min
LeetCode Easy Tree Depth-First-Search Breadth-First-Search Binary-Tree
LeetCode 404 note: tell each child during DFS whether it is a left child, adding only left children that are leaves
LeetCode 226: Invert Binary Tree
·1 min
LeetCode Easy Tree Depth-First-Search Breadth-First-Search Binary-Tree
LeetCode 226 note: swap every node’s children, using a simultaneous assignment so the already-changed side is not reused