-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path530.cpp
More file actions
31 lines (30 loc) · 688 Bytes
/
Copy path530.cpp
File metadata and controls
31 lines (30 loc) · 688 Bytes
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
// inOrderTraversal.cpp
/**
* 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 {
int min;
int lastValue;
public:
int getMinimumDifference(TreeNode *root) {
min = INT_MAX;
lastValue = -1;
getMinimum(root);
return min;
}
void getMinimum(TreeNode *root) {
if (root->left != NULL)
getMinimum(root->left);
if (lastValue >= 0 && abs(lastValue - root->val) < min)
min = abs(lastValue - root->val);
lastValue = root->val;
if (root->right != NULL)
getMinimum(root->right);
}
};