- Leetcode PHP题解--D96 530. Minimum
- 530. Minimum Absolute Difference
- 530. Minimum Absolute Difference
- 530. Minimum Absolute Difference
- 530. Minimum Absolute Difference
- Minimum Absolute Difference in B
- 530. Minimum Absolute Differenc
- [LeetCode]530. Minimum Absolute
- 530.二叉搜索树的最小绝对差
- [刷题防痴呆] 0530 - 二叉搜索树的最小绝对差 (Mini
BST的中序遍历是一个有序数组!!!!
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
int getMinimumDifference(TreeNode* root) {
vector<int>ans;
inorder(root, ans);
int mindif = INT_MAX;
for(int i = 1; i < ans.size(); i++){
if(abs(ans[i] - ans[i-1]) < mindif){
mindif = abs(ans[i] - ans[i-1] );
}
}
return mindif;
}
private:
void inorder(TreeNode* root, vector<int>& num){
if(root == NULL){
return;
}
inorder(root->left, num);
num.push_back(root->val);
inorder(root->right,num);
}
};
网友评论