-
Notifications
You must be signed in to change notification settings - Fork 0
/
D_Permutation_Transformation.cpp
75 lines (72 loc) · 1.57 KB
/
D_Permutation_Transformation.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
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
73
74
75
#include<bits/stdc++.h>
using namespace std;
struct TreeNode{
int val;
TreeNode *left,*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) {}
};
TreeNode* buildTree(int s,int e,vector<int>& permute)
{
if(s>=e)
return NULL;
int t_max=permute[s];
int idx=s;
for(int i=s;i<e;i++)
{
if(permute[i]>t_max)
{
t_max=permute[i];
idx=i;
}
}
TreeNode* root=new TreeNode(t_max);
root->left=buildTree(s,idx,permute);
root->right=buildTree(idx+1,e,permute);
return root;
}
int main()
{
int t;
cin >> t;
for(int it=1;it<=t;it++) {
int n;
cin>>n;
vector<int>permute(n);
for(int &i:permute)
cin>>i;
vector<int>depth;
TreeNode* root=buildTree(0,n,permute);
queue<TreeNode*>q;
unordered_map<int,int>findDepth;
q.push(root);
int level=0;
while(!q.empty())
{
int len=q.size();
for(int i=0;i<len;i++)
{
TreeNode* curr=q.front();
q.pop();
findDepth[curr->val]=level;
if(curr->left)
q.push(curr->left);
if(curr->right)
q.push(curr->right);
}
level++;
}
for(int i=0;i<n;i++)
{
int val=findDepth[permute[i]];
depth.push_back(val);
}
for(int i=0;i<n;i++)
{
cout<<depth[i]<<" ";
}
cout<<endl;
}
return 0;
}