-
Notifications
You must be signed in to change notification settings - Fork 1
/
530.cpp
40 lines (36 loc) · 881 Bytes
/
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
33
34
35
36
37
38
39
40
#include <bits/stdc++.h>
using namespace std;
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
struct TreeNode {
int val;
TreeNode *left;
TreeNode *right;
TreeNode(int x) : val(x), left(NULL), right(NULL) {}
};
class Solution {
public:
int min_diff;
int pre_diff;
int getMinimumDifference(TreeNode* root) {
this->min_diff = INT_MAX;
this->pre_diff = -100000000;
inorder(root);
return min_diff;
}
void inorder(TreeNode* root) {
if (root == nullptr) return;
inorder(root->left);
if ((root->val - pre_diff) < min_diff)
min_diff = root->val - pre_diff;
pre_diff = root->val;
inorder(root->right);
}
};