-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathminimum_absolute_diference.cpp
More file actions
38 lines (36 loc) · 1.06 KB
/
minimum_absolute_diference.cpp
File metadata and controls
38 lines (36 loc) · 1.06 KB
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
//program to find out minimum absolute deference in a BST
//problem link: https://leetcode.com/problems/minimum-absolute-difference-in-bst/
/**
* 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:
vector<int>v;
void get_min(TreeNode * root)
{
if(root == NULL) return;//base case
get_min(root -> left);
v.push_back(root -> val);
get_min(root -> right);
}
int getMinimumDifference(TreeNode* root) {
if(root == NULL) return 0;
else if(root -> left == NULL && root -> right == NULL) return abs(root -> val);
v.clear();
get_min(root);
int mn = INT_MAX;
for(int i = v.size() -1; i>0; i--)
{
mn = min(mn,v[i] - v[i-1]);
}
return abs(mn);
}
};