-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBinary Tree Serialization.cpp
95 lines (87 loc) · 2.31 KB
/
Binary Tree Serialization.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
#include <iostream>
#include <vector>
#include <stack>
#include <queue>
using namespace std;
/**
* Definition for a binary tree node.
*/
struct TreeNode {
int val;
TreeNode *left;
TreeNode *right;
TreeNode(int x) : val(x), left(NULL), right(NULL) {}
};
struct RESULT {
int nmaxDepth;
int nmaxPath;
};
class Codec {
public:
/**
* This method will be invoked first, you should design your own algorithm
* to serialize a binary tree which denote by a root node to a string which
* can be easily deserialized by your own "deserialize" method later.
*/
void serializeHelp(TreeNode *root, ostringstream &ss) {
if(root == NULL) {
ss << "# ";
}
else {
ss << root->val << " ";
serializeHelp(root->left, ss);
serializeHelp(root->right, ss);
}
}
string serialize(TreeNode *root) {
// write your code here
ostringstream out;
serializeHelp(root, out);
string res = out.str();
return res;
}
/**
* This method will be invoked second, the argument data is what exactly
* you serialized at method "serialize", that means the data is not given by
* system, it's given by your own serialize method. So the format of data is
* designed by yourself, and deserialize it here as you serialize it in
* "serialize" method.
*/
TreeNode *deserializeHelp(istringstream &in) {
string val;
in >> val;
if(val == "#") return NULL;
TreeNode *root = new TreeNode(stoi(val));
root->left = deserializeHelp(in);
root->right = deserializeHelp(in);
return root;
}
TreeNode *deserialize(string data) {
// write your code here
istringstream in(data);
return deserializeHelp(in);
}
};
TreeNode *buildTree(int depth, int &val) {
TreeNode *root = new TreeNode(val);
if(depth == 0) {
return root;
}
root->left = buildTree(depth - 1, ++val);
root->right = buildTree(depth - 1, ++val);
return root;
}
int main() {
int val = 1;
TreeNode *tree = buildTree(3, val);
Solution s;
vector<vector<int> > res;
res = s.zigzagLevelOrder(tree);
for(int i = 0; i < res.size(); i++) {
for(int j = 0; j < res[i].size(); j++) {
cout << res[i][j] << " ";
}
cout << endl;
}
return 0;
}