Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit cfa9f08

Browse files
committed
"Serialize and Deserialize Binary Tree"
1 parent 5162f41 commit cfa9f08

File tree

2 files changed

+134
-0
lines changed

2 files changed

+134
-0
lines changed

‎README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ The `☢` means that you need to have a LeetCode Premium Subscription.
2525

2626
| | Problem | Solution |
2727
| --- | ------------------------------------------------------------ | ------------------ |
28+
| 297 | [Serialize and Deserialize Binary Tree] | [C++](src/297.cpp) |
2829
| 296 | [Best Meeting Point]| |
2930
| 295 | [Find Median from Data Stream] | [C](src/295.c) |
3031
| 294 | [Flip Game II]| |
@@ -309,6 +310,7 @@ The `☢` means that you need to have a LeetCode Premium Subscription.
309310
[LeetCode algorithm problems]: https://leetcode.com/problemset/algorithms/
310311

311312

313+
[Serialize and Deserialize Binary Tree]: https://leetcode.com/problems/serialize-and-deserialize-binary-tree/
312314
[Best Meeting Point]: https://leetcode.com/problems/best-meeting-point/
313315
[Find Median from Data Stream]: https://leetcode.com/problems/find-median-from-data-stream/
314316
[Flip Game II]: https://leetcode.com/problems/flip-game-ii/

‎src/297.cpp

Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
#include <iostream>
2+
#include <queue>
3+
#include <string>
4+
#include <cassert>
5+
6+
using namespace std;
7+
8+
struct TreeNode {
9+
int val;
10+
TreeNode *left;
11+
TreeNode *right;
12+
TreeNode(int x) : val(x), left(NULL), right(NULL) {}
13+
};
14+
15+
class Codec {
16+
public:
17+
// Encodes a tree to a single string.
18+
string serialize(TreeNode* root) {
19+
if (root == NULL) return "";
20+
queue<TreeNode *> q;
21+
q.push(root);
22+
TreeNode *p;
23+
string result = to_string(root->val);
24+
while (!q.empty()) {
25+
p = q.front();
26+
q.pop();
27+
if (p->left && p->right) {
28+
q.push(p->left);
29+
q.push(p->right);
30+
result += "," + to_string(p->left->val) + "," + to_string(p->right->val);
31+
}
32+
else if (p->left && !p->right) {
33+
q.push(p->left);
34+
result += ",L," + to_string(p->left->val);
35+
}
36+
else if (!p->left && p->right) {
37+
q.push(p->right);
38+
result += ",R," + to_string(p->right->val);
39+
}
40+
else {
41+
result += ",N";
42+
}
43+
}
44+
return result;
45+
}
46+
47+
// Decodes your encoded data to tree.
48+
TreeNode* deserialize(string data) {
49+
if (data.length() == 0) return NULL;
50+
vector<string> nodes = split(data, ',');
51+
auto it = nodes.begin();
52+
TreeNode *root = new TreeNode(stoi(*(it++)));
53+
queue<TreeNode *> q;
54+
q.push(root);
55+
while (!q.empty() && it != nodes.end()) {
56+
TreeNode *p = q.front();
57+
if (*it == "N") {
58+
it++;
59+
}
60+
else if (*it == "L") {
61+
p->left = new TreeNode(stoi(*(++it)));
62+
it++;
63+
}
64+
else if (*it == "R") {
65+
p->right = new TreeNode(stoi(*(++it)));
66+
it++;
67+
}
68+
else {
69+
p->left = new TreeNode(stoi(*(it++)));
70+
p->right = new TreeNode(stoi(*(it++)));
71+
}
72+
73+
q.pop();
74+
if (p->left) q.push(p->left);
75+
if (p->right) q.push(p->right);
76+
}
77+
return root;
78+
}
79+
80+
vector<string> split(string str, char delimiter) {
81+
vector<string> result;
82+
int last = 0;
83+
for (int i = 0; i <= str.length(); i++) {
84+
if (str[i] == delimiter || str[i] == '0円') {
85+
result.push_back(str.substr(last, i - last));
86+
last = i + 1;
87+
}
88+
}
89+
return result;
90+
}
91+
};
92+
93+
bool compareTree(TreeNode *a, TreeNode *b) {
94+
if (a == NULL && b == NULL) return true;
95+
if (a == NULL || b == NULL) return false;
96+
97+
return (a->val == b->val)
98+
&& compareTree(a->left, b->left)
99+
&& compareTree(a->right, b->right);
100+
}
101+
102+
// Your Codec object will be instantiated and called as such:
103+
// Codec codec;
104+
// codec.deserialize(codec.serialize(root));
105+
106+
int main() {
107+
TreeNode *root = new TreeNode(1);
108+
root->left = new TreeNode(2);
109+
root->right = new TreeNode(3);
110+
root->right->left = new TreeNode(4);
111+
root->right->right = new TreeNode(5);
112+
113+
root->right->left->left = new TreeNode(6);
114+
root->right->right->right = new TreeNode(7);
115+
116+
// 1
117+
// / \
118+
// 2 3
119+
// / \
120+
// 4 5
121+
// / \
122+
// 6 7
123+
// serialization: 1,2,N,3,4,5,L,6,R,7,N,N
124+
125+
Codec codec;
126+
TreeNode *new_root = codec.deserialize(codec.serialize(root));
127+
128+
assert(compareTree(root, new_root) == true);
129+
printf("all tests passed!\n");
130+
131+
return 0;
132+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /