-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path60.h
67 lines (57 loc) · 1.32 KB
/
60.h
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
/*
* @Author: FreedomLy
* @Date: 2018-05-07 19:49:12
* @Last Modified by: FreedomLy
* @Last Modified time: 2018-05-07 20:06:11
* 题目
* 实现两个函数,分别用来序列化和反序列化二叉树
*/
// 思路
// 采用前序遍历,当左右子节点为nullptr时,采用'#'代替
#pragma once
#include <string>
#include <sstream>
#include "TreeNode.h"
using std::string;
using std::ostringstream;
using std::istringstream;
using tree::TreeNode;
class Solution60 {
public:
const string serialize(TreeNode* root)
{
ostringstream out;
serialize(root, out);
return out.str();
}
TreeNode* deserialize(const string& str)
{
istringstream in(str);
return deserialize(in);
}
private:
void serialize(TreeNode* root, ostringstream& out)
{
if (root)
{
out << root->val << " ";
serialize(root->left, out);
serialize(root->right, out);
}
else
{
out << "# ";
}
}
TreeNode* deserialize(istringstream& in)
{
string val;
in >> val;
if (val == "#")
return nullptr;
TreeNode* root = new TreeNode(stoi(val));
root->left = deserialize(in);
root->right = deserialize(in);
return root;
}
};