-
Notifications
You must be signed in to change notification settings - Fork 0
/
1117.cpp
66 lines (62 loc) · 1.15 KB
/
1117.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
#include <iostream>
#include <stack>
#include <queue>
using namespace std;
struct node
{
int data;
node * son;
node * bro;
node(int n=0, node *a = NULL, node *b = NULL)
{
data = n;
son = a;
bro = b;
}
};
node *root;
void create_tree()
{
stack<node *> s;
char tmp;
cin>>tmp;
cin>>tmp;
root = new node(tmp-'0');
s.push(root);
while(true)
{
cin>>tmp;
if(tmp=='(')
{
cin>>tmp;
node * tmp2 = s.top();
s.pop();
if(tmp2->son == NULL)
{
tmp2->son = new node(tmp-'0');
s.push(tmp2);
s.push(tmp2->son);
}
else
{
node *tmp3 = tmp2->son;
while(tmp3->bro!=NULL)
tmp3 = tmp3->bro;
tmp3->bro = new node(tmp-'0');
s.push(tmp2);
s.push(tmp3->bro);
}
}
else if(tmp==')')
{
s.pop();
if(s.empty())
break;
}
}
}
int main()
{
create_tree();
return 0;
}