{"type": "session", "harness": "opencode", "id": "cp-10037", "name": "LeetCode 0530: Minimum Absolute Difference In Bst"} {"type": "message", "message": {"role": "system", "content": "You are an expert LeetCode problem solver. You solve problems efficiently with clean code."}} {"type": "message", "message": {"role": "user", "content": "Problem: Minimum Absolute Difference in BST (Difficulty: Easy)\nTags: Tree, Depth-First Search, Breadth-First Search, Binary Search Tree, Binary Tree\n\nGiven the root of a Binary Search Tree (BST), return the minimum absolute difference between the values of any two different nodes in the tree.\nNote: This question is the same as 783: https://leetcode.com/problems/minimum-distance-between-bst-nodes/\n\nExample 1:\nInput: root = [4,2,6,1,3]\nOutput: 1\n\nExample 2:\nInput: root = [1,0,48,null,null,12,49]\nOutput: 1\n\nConstraints:\n- The number of nodes in the tree is in the range [2, 104].\n- 0 <= Node.val <= 10^5"}} {"type": "message", "message": {"role": "assistant", "content": "# Time: O(n)\n# Space: O(h)\n\nclass Solution(object):\n def getMinimumDifference(self, root):\n \"\"\"\n :type root: TreeNode\n :rtype: int\n \"\"\"\n def inorderTraversal(root, prev, result):\n if not root:\n return (result, prev)\n\n result, prev = inorderTraversal(root.left, prev, result)\n if prev: result = min(result, root.val - prev.val)\n return inorderTraversal(root.right, root, result)\n\n return inorderTraversal(root, None, float(\"inf\"))[0]\n\n"}}