-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathp4.cpp
More file actions
52 lines (42 loc) · 1.04 KB
/
Copy pathp4.cpp
File metadata and controls
52 lines (42 loc) · 1.04 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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#include<iostream>
using namespace std;
struct Node{
Node(int data=0,Node *child=nullptr,Node *sibing=nullptr):data(data),child(child),sibing(sibing){}
int data;
Node *child,*sibing;
};
Node* create(istream &in){
int n;
if(!(in>>n)) return nullptr;
Node** nodes=new Node*[n+1];
for(int i=1;i<=n;i++)
nodes[i]=new Node(i);
for(int i=1;i<=n;i++){
int k; in>>k;
if(k==0) continue;
int ch; in>>ch;
nodes[i]->child=nodes[ch];
Node* lstsib=nodes[i]->child;
for(int j=2;j<=k;j++)
cin>>ch,
lstsib->sibing=nodes[ch],
lstsib=lstsib->sibing;
}
return nodes[1];
}
void TraverseLevelI(Node* tree,int i){
if(i==1)
for(;tree;tree=tree->sibing)
cout<<tree->data<<" ";
else
for(;tree;tree=tree->sibing)
TraverseLevelI(tree->child,i-1);
}
int main(){
Node* tree=create(cin);
int i; cin>>i;
cout<<"Traverse level: ";
TraverseLevelI(tree,i);
cout<<endl;
return 0;
}