Advertisement

Minimum Distance Between BST Nodes - LeetCode 783 Solution

Minimum Distance Between BST Nodes - Complete Solution Guide

Minimum Distance Between BST Nodes is LeetCode problem 783, a Easy level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.

Problem Statement

Given the root of a Binary Search Tree (BST), return the minimum 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, 100] . 0 <= Node.val <= 10 5 Note: This question is the same as 530: https://leetcode.com/problems/minimum-absolute-difference-in-bst/

Detailed Explanation

The problem asks us to find the minimum difference between the values of any two distinct nodes in a given Binary Search Tree (BST). A BST has the property that for any node, all nodes in its left subtree have values less than the node's value, and all nodes in its right subtree have values greater than the node's value.

Solution Approach

The provided solutions leverage the inorder traversal property of BSTs. They perform an inorder traversal, keeping track of the previously visited node. During the traversal, the absolute difference between the current node's value and the previous node's value is calculated. The minimum of all these differences is then returned.

Step-by-Step Algorithm

  1. Step 1: Initialize a variable `min_diff` (or `minDiff`) to infinity (or maximum integer value). This variable will store the minimum difference found so far.
  2. Step 2: Initialize a variable `prev` to negative infinity (or null if using Integer object). This variable will store the value of the previously visited node during the inorder traversal.
  3. Step 3: Perform an inorder traversal of the BST. In the Python version, an iterative inorder traversal is used with a stack. The Java and CPP versions use recursive inorder traversal.
  4. Step 4: During the inorder traversal, for each node, calculate the difference between the current node's value and the value of the `prev` node.
  5. Step 5: Update `min_diff` with the minimum of its current value and the calculated difference. For the first node, prev will be negative infinity (or null) so the difference is not valid. This is why there is an 'if' condition to proceed with calculating the min_diff.
  6. Step 6: Update `prev` to the current node's value.
  7. Step 7: After the traversal is complete, return `min_diff`.

Key Insights

  • The inorder traversal of a BST yields a sorted sequence of node values.
  • The minimum difference between any two nodes in a BST will occur between two adjacent nodes in the sorted order.
  • Iterative or recursive inorder traversal can be used to generate the sorted sequence.

Complexity Analysis

Time Complexity: O(n)

Space Complexity: O(n)

Topics

This problem involves: Tree, Depth-First Search, Breadth-First Search, Binary Search Tree, Binary Tree.

Companies

Asked at: Wix.