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:
[2, 104].0 <= Node.val <= 105Note: This question is the same as 783: https://leetcode.com/problems/minimum-distance-between-bst-nodes/
When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
The brute force method for finding the minimum absolute difference in a binary search tree involves checking every possible pair of node values. It's like comparing each person in a room with every other person to find the two closest in age, without any clever shortcuts.
Here's how the algorithm would work step-by-step:
def minimum_absolute_difference_bst_brute_force(root):
node_values = []
def inorder_traversal(node):
if node:
inorder_traversal(node.left)
node_values.append(node.val)
inorder_traversal(node.right)
inorder_traversal(root)
minimum_difference = float('inf')
# Iterate through all node values to calculate difference
for first_index in range(len(node_values)):
# Compare the first value with all others.
for second_index in range(len(node_values)):
if first_index != second_index:
# Ensure we're only computing absolute differences.
current_difference = abs(node_values[first_index] - node_values[second_index])
# Update min difference if smaller value is found
if current_difference < minimum_difference:
minimum_difference = current_difference
return minimum_differenceThe key to solving this problem efficiently lies in understanding the properties of a Binary Search Tree (BST). Since a BST is ordered, we can traverse the tree in a way that allows us to compare adjacent nodes and efficiently find the minimum difference.
Here's how the algorithm would work step-by-step:
class TreeNode:
def __init__(self, val=0, left=None, right=None):
self.val = val
self.left = left
self.right = right
def get_minimum_difference(root):
minimum_absolute_difference = float('inf')
previous_node_value = None
def inorder_traversal(node):
nonlocal minimum_absolute_difference
nonlocal previous_node_value
if not node:
return
inorder_traversal(node.left)
# Only calculate diff after first node
if previous_node_value is not None:
difference = abs(node.val - previous_node_value)
# Update the minimum difference found
minimum_absolute_difference = min(minimum_absolute_difference, difference)
# Store current node value for next comp
previous_node_value = node.val
inorder_traversal(node.right)
inorder_traversal(root)
return minimum_absolute_difference| Case | How to Handle |
|---|---|
| Null or empty tree | Return a large value like Integer.MAX_VALUE since no difference can be computed, or handle separately by returning a special value or throwing an exception if appropriate. |
| Tree with only one node | Return a large value like Integer.MAX_VALUE since no difference can be computed. |
| Tree with only two nodes | Directly compute the absolute difference between the two node values. |
| BST with all nodes having the same value | The minimum absolute difference will be 0, which the inorder traversal approach will find when comparing adjacent identical values. |
| Highly skewed BST (e.g., linked list) | Inorder traversal will still correctly find the minimum difference between adjacent nodes, but recursion depth could be a concern, suggesting iterative inorder traversal. |
| BST with negative node values | Absolute difference calculation handles negative numbers correctly. |
| Large range of node values (potential integer overflow) | Ensure that the absolute difference calculation uses long to prevent integer overflow issues. |
| Very large BST | The inorder traversal approach has O(N) time complexity and O(H) space complexity where H is height of the tree, which scales reasonably; however, for extremely deep trees, consider iterative inorder to reduce stack usage. |