[LeetCode] 530. Minimum Absolute Difference in BST
2017-08-05 18:09
330 查看
Given a binary search tree with non-negative values, find the minimum absolute difference between values of any two nodes.
Example:
Example:
Input: 1 \ 3 / 2 Output: 1 Explanation: The minimum absolute difference is 1, which is the difference between 2 and 1 (or between 2 and 3).
class Solution { public: int getMinimumDifference(TreeNode* root) { stack<TreeNode *> stk; int MinAbsDiff = INT_MAX, lastval = -1; TreeNode *ptn = root; while (!stk.empty() || ptn) { if (ptn) { stk.push(ptn); ptn = ptn->left; } else { ptn = stk.top(); stk.pop(); if (lastval == -1) lastval = ptn->val; else { MinAbsDiff = min(MinAbsDiff, abs(ptn->val - lastval)); lastval = ptn->val; } ptn = ptn->right; } } return MinAbsDiff; } };
相关文章推荐
- [Leetcode] 530. Minimum Absolute Difference in BST 解题报告
- LeetCode 530. Minimum Absolute Difference in BST
- LeetCode-530. Minimum Absolute Difference in BST (Java)
- LeetCode 530. Minimum Absolute Difference in BST
- LeetCode - 530. Minimum Absolute Difference in BST
- leetcode 530. Minimum Absolute Difference in BST
- [LeetCode]530. Minimum Absolute Difference in BST
- leetcode 530. Minimum Absolute Difference in BST
- Leetcode530. Minimum Absolute Difference in BST
- leetcode 530. Minimum Absolute Difference in BST
- [LeetCode]530. Minimum Absolute Difference in BST
- leetcode 530. Minimum Absolute Difference in BST
- leetcode-530. Minimum Absolute Difference in BST
- LeetCode 530. Minimum Absolute Difference in BST (二叉搜索树中最小绝对差)
- 【Leetcode】530. Minimum Absolute Difference in BST
- LeetCode-530. Minimum Absolute Difference in BST
- [LeetCode]530. Minimum Absolute Difference in BST(BST的最小绝对差)
- [leetcode]-530. Minimum Absolute Difference in BST
- Leetcode-530. Minimum Absolute Difference in BST
- LeetCode | 530. Minimum Absolute Difference in BST