530. Minimum Absolute Difference in BST

problem

solution

option 1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
* };
*/
class Solution {
public:
void inorder(TreeNode *root, vector<int> & ret){
if(!root) return ;

inorder(root->left, ret);
ret.push_back(root->val);
inorder(root->right, ret);
}
int getMinimumDifference(TreeNode* root) {
vector<int> ret;
inorder(root, ret);
// ret is increasing array
int ans = ret[1] - ret[0], n= ret.size();
for(int i=2;i<n;++i){
ans = min(ans, ret[i] - ret[i-1]);
}
return ans;
}
};

option 2

需要多一個節點或是int大小空間紀錄上一次拜訪到的節點

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
class Solution {
public:
void inorder(TreeNode *root, TreeNode* & prev, int &diff){
if(!root) return;


inorder(root->left, prev, diff);

// 最小值位置
if(prev == nullptr){
prev = root;
}
else{
diff = min(root->val - prev->val, diff);
// 拜訪完,要更新prev
prev = root;
}


inorder(root->right,prev, diff);
}
int getMinimumDifference(TreeNode* root) {
int diff = INT_MAX;
// pre 存取上一個拜訪的節點
TreeNode *pre = nullptr;
inorder(root,pre, diff);
return diff;
}
};