문제 출처 : https://www.acmicpc.net/problem/1991

 

<C++Code>--1

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
#include<iostream>
#include<string>
#define number 1000
#define endl '\n'
using namespace std;
typedef class point *treeptr;
 
class point{
    public:
    string date;
    treeptr left,right;
};
void preorder(treeptr ptr)
{
    if(ptr&&ptr->date!=".")
    {
        cout<<ptr->date;
        preorder(ptr->left);
        preorder(ptr->right);
    }
}
void inorder(treeptr ptr)
{
    if(ptr&&ptr->date!=".")
    {
        
     inorder(ptr->left);
     cout<<ptr->date;
         inorder(ptr->right);
    }
}
void postorder(treeptr ptr)
{
    if(ptr&&ptr->date!=".")
    {
        postorder(ptr->left);
        postorder(ptr->right);
        cout<<ptr->date;
    }
}
int n;
int main(void)
{
    ios::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
    cin>>n;
    point po[number];
    for(int i=0;i<=number;i++)
    {
        if(i%2==0)
        {
            po[i/2].left=&po[i];
        }
        else
        {
            po[i/2].right=&po[i];
        }
    }
    po[1].date='A';
    
    while(n--)
    {
        string q,w,e;
        cin>>q>>w>>e;
        int index=0;
        for(int i=1;i<=number;i++)
        {
            if(po[i].date==q)
            {
                index=i;
                break;
            }
        }
        po[index].left->date=w;
        po[index].right->date=e;
    }
    
    preorder(&po[1]);
    cout<<endl;
    inorder(&po[1]);
    cout<<endl;
    postorder(&po[1]);
    return 0;
    
}
cs

<C++Code>--2

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
#include<iostream>
#define max 100001
using namespace std;
 
class point{
    public:
        char date;
        int left,right;
};
point po[max];
void preorder(int x)
{
        if(x==-1return ;
    
        cout<<char(x+'A');
        preorder(po[x].left);
        preorder(po[x].right);
    
}
void inorder(int x)
{
    if(x==-1return  ;
    inorder(po[x].left);
    cout<<char(x+'A');
    inorder(po[x].right);
}
void postorder(int x)
{
    if(x==-1return ;
    postorder(po[x].left);
    postorder(po[x].right);
    cout<<char(x+'A');
}
 
 
int main(void)
{
    int n;
    cin>>n;
    for(int i=0;i<n;i++)
    {
        char q,w,e;
        cin>>q>>w>>e;
        int x=q-'A';
    
        if(w=='.')
        {
            po[x].left=-1;
        }
        else
        {
            po[x].left=w-'A';
        }
        if(e=='.')
        {
            po[x].right=-1;
        }
        else
        {
            po[x].right=e-'A';
        }
        
    }
    preorder(0);
    cout<<endl;
    inorder(0);
    cout<<endl;
    postorder(0);
    return 0;
    
}
cs

'백준 온라인 저지 > 트리' 카테고리의 다른 글

1967번_트리의 지름(2)  (0) 2019.12.18
1167번_트리의 지름  (0) 2019.12.17
2250번_트리의 높이와 너비  (0) 2019.12.16
11725번_트리의 부모 찾기(Dfs)  (0) 2019.12.09

+ Recent posts