Minimum Absolute Difference in BST
Given the root of a BST, return the minimum absolute difference between the values of any two different nodes in the tree. Use the property that in-order traversal of a BST is sorted.
Why Interviewers Ask This
Uber asks this question to evaluate a candidate's ability to leverage fundamental data structure properties for optimization. Specifically, they assess whether you recognize that an in-order traversal of a Binary Search Tree yields a sorted sequence. This tests your capacity to reduce a naive O(N^2) comparison problem to an efficient O(N) linear scan by utilizing the inherent sorted nature of BSTs rather than brute-forcing all pairs.
How to Answer This Question
Key Points to Cover
- Explicitly identifying that in-order traversal creates a sorted array
- Reducing the problem from O(N^2) to O(N) by only checking adjacent elements
- Demonstrating awareness of space complexity regarding recursion depth
- Handling edge cases like single-node trees or null inputs correctly
- Clearly articulating the logic flow without writing code immediately
Sample Answer
Common Mistakes to Avoid
- Attempting to compare every node with every other node, leading to inefficient O(N^2) solutions
- Forgetting to initialize the 'previous' node tracker correctly, causing incorrect calculations on the first few nodes
- Ignoring the sorted property of BSTs and treating it as a generic binary tree requiring full pairwise comparison
- Overlooking edge cases such as trees with only one node or negative integer values
Practice This Question with AI
Answer this question orally or via text and get instant AI-powered feedback on your response quality, structure, and delivery.