-
Notifications
You must be signed in to change notification settings - Fork 127
/
Shortest Range In BST
72 lines (58 loc) · 1.79 KB
/
Shortest Range In BST
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
class Solution{
public:
int findDepth(Node *root)
{
if(root==NULL)
return 0;
return 1 + max(findDepth(root->left), findDepth(root->right));
}
pair<int, int> shortestRange(Node *root) {
// Your code goes here
queue<Node*> q;
q.push(root);
int l = root->data, r = root->data;
int d = findDepth(root);
vector<vector<int>> nums(d);
int i = 0 ;
while(!q.empty())
{
int size = q.size();
while(size--)
{
Node* x = q.front();
q.pop();
nums[i].push_back(x->data);
if(x->left)
q.push(x->left);
if(x->right)
q.push(x->right);
}
++i;
}
pair<int, int> ans;
int gap = INT_MAX;
set<pair<int, pair<int, int>>> st;
for (int i = 0; i < nums.size(); ++i)
{
st.insert({nums[i][0], {i, 0}});
}
while (1)
{
pair<int, pair<int, int>> mini = *st.begin();
auto it = st.end();
--it;
pair<int, pair<int, int>> maxi = *it;
st.erase(st.begin());
if (maxi.first - mini.first < gap)
{
gap = maxi.first - mini.first;
ans.first = mini.first;
ans.second = maxi.first;
}
if (mini.second.second + 1 >= nums[mini.second.first].size())
break;
st.insert({nums[mini.second.first][mini.second.second + 1], {mini.second.first, mini.second.second + 1}});
}
return ans;
}
};