-
Notifications
You must be signed in to change notification settings - Fork 0
/
530.hpp
39 lines (32 loc) · 827 Bytes
/
530.hpp
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
#ifndef LEETCODE_530_HPP
#define LEETCODE_530_HPP
#include <iostream>
#include <queue>
#include <algorithm>
#include <vector>
#include <unordered_map>
#include <unordered_set>
#include <set>
#include <numeric>
#include <stack>
#include <string>
#include "../common/leetcode.hpp"
using namespace std;
class Solution {
public:
int getMinimumDifference(TreeNode *root) {
if (root == nullptr)
return min_diff;
if (root->left != nullptr)
getMinimumDifference(root->left);
if (val >= 0)
min_diff = min(min_diff, root->val - val);
val = root->val;
if (root->right != nullptr)
getMinimumDifference(root->right);
return min_diff;
}
private:
int min_diff{numeric_limits<int>::max()}, val{-1};
};
#endif //LEETCODE_530_HPP