-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path02may.txt
73 lines (63 loc) · 1.47 KB
/
02may.txt
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
class Solution
{
public:
vector<int> serialize(Node *root)
{
vector<int> v;
queue<Node*> q;
q.push(root);
while(!q.empty())
{
Node* node=q.front();
q.pop();
if(node)
v.push_back(node->data);
else
v.push_back(-1);
if(node)
{
q.push(node->left);
q.push(node->right);
}
}
return v;
}
Node * deSerialize(vector<int> &A)
{
Node* root=new Node(A[0]);
queue<Node*> q;
q.push(root);
int i=1;
while(!q.empty() and i<A.size())
{
Node* node=q.front();
q.pop();
bool FR=0;
bool FL=0;
if(A[i]==-1)
{
node->left=NULL;
FL=1;
}
if(A[i+1]==-1)
{
node->right=NULL;
FR=1;
}
if(!FL)
{
Node* newNode1=new Node(A[i]);
node->left=newNode1;
q.push(newNode1);
}
if(!FR)
{
Node* newNode1=new Node(A[i+1]);
node->right=newNode1;
q.push(newNode1);
}
i+=2;
}
return root;
}
};