-
Notifications
You must be signed in to change notification settings - Fork 0
/
W7_PostOrderDepth4.cpp
98 lines (88 loc) · 2.13 KB
/
W7_PostOrderDepth4.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
//Created by dongwan-kim on 2022/04/13.
#include<iostream>
#include<vector>
using namespace std;
vector<int> nodeV;
vector<int> nodeD;
int t,n;
class Node{
public:
Node *parent;
int value;
vector<Node*> childList;
Node(){
parent= nullptr;
value=0;
}
Node(Node *parentN,int V){
parent=parentN;
value=V;
}
};
class Tree{
public:
Node *root;
Tree(){
root=new Node();
}
void makeTree(){
int depth=-1;
Node *curNode=root;
for(int i=nodeD.size()-1;i>=0;i--){
if(nodeD[i]==0){
root->value=1;
depth=nodeD[i];
}
else{
if(nodeD[i]>depth){
Node *newNode=new Node(curNode,nodeV[i]);
curNode->childList.push_back(newNode);
curNode=newNode;
}
else if(nodeD[i]==depth){
Node *newNode=new Node(curNode->parent,nodeV[i]);
curNode->parent->childList.push_back(newNode);
curNode=newNode;
}
else if(nodeD[i]<depth){
while(depth!=nodeD[i]){
curNode=curNode->parent;
depth--;
}
Node *newNode=new Node(curNode->parent,nodeV[i]);
curNode->parent->childList.push_back(newNode);
curNode=newNode;
}
depth=nodeD[i];
}
}
}
void preOrder(Node* r){
cout<<r->value<<" ";
for(int i=r->childList.size()-1;i>=0;i--){
preOrder(r->childList[i]);
}
}
};
int main(){
cin>>t;
while(t--){
Tree tree;
nodeD.clear();
nodeV.clear();
cin>>n;
for(int i=0;i<n;i++){
int a;
cin>>a;
nodeV.push_back(a);
}
for(int i=0;i<n;i++){
int a;
cin>>a;
nodeD.push_back(a);
}
tree.makeTree();
tree.preOrder(tree.root);
cout<<endl;
}
}