-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path530.cpp
32 lines (32 loc) · 1.03 KB
/
530.cpp
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
32
/**
* 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 inOrderAux(TreeNode* root, vector<int>& res) {
if (!root) return;
if (root->left) inOrderAux(root->left, res);
res.push_back(root->val);
if (root->right) inOrderAux(root->right, res);
}
int getMinimumDifference(TreeNode* root) {
vector<int> inOrder;
inOrderAux(root, inOrder);
int min = inOrder[1] - inOrder[0];
for (int i = 2; i < inOrder.size(); i++) {
int diff = inOrder[i] - inOrder[i-1];
if (diff < min) {
min = diff;
}
}
return min;
}
};