Leetcode Problem 530. Minimum Absolute Difference in BST

530. Minimum Absolute Difference in BST

Leetcode Solutions

Approach: In-order Traversal Without List

  1. Initialize minDifference to infinity and prevNode to null.
  2. Define a recursive function inorderTraversal(node) that updates prevNode and minDifference.
    • If node is null, return.
    • Recursively call inorderTraversal(node.left).
    • If prevNode is not null, update minDifference with the difference between node.val and prevNode.val.
    • Set prevNode to node.
    • Recursively call inorderTraversal(node.right).
  3. Start the in-order traversal by calling inorderTraversal(root).
  4. Return minDifference after the traversal is complete.
UML Thumbnail

Approach: Depth First Search

Ask Question

Programming Language
image/screenshot of info(optional)
Full Screen
Loading...

Suggested Answer

Answer
Full Screen
Copy Answer Code
Loading...