本题考点:
http://blog.csdn.net/weiwei22844/article/details/42004983
leetcode题目地址
297. Serialize and Deserialize Binary Tree
https://leetcode.com/problems/serialize-and-deserialize-binary-tree/
层次遍历,ac代码,注意反序列化如何操作??
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Codec {
public:
// Encodes a tree to a single string. 层次遍历
string serialize(TreeNode* root) {
string ans;
if (root == NULL)
return "[]";
ans = "[";
queue que;
que.push(root);
char str[100];
while (!que.empty())
{
TreeNode* top = que.front();
que.pop();
if (top != NULL)
{
que.push(top->left);
que.push(top->right);
sprintf(str, "%d", top->val);
ans += str;
}
else{
ans += "null";
}
ans += ",";
}
int end = ans.length() - 1;
while ( !( ans[end] >= '0' && ans[end] <= '9') )
end--;
string rs = ans.substr(0, end + 1);
rs += "]";
return rs;
}
// Decodes your encoded data to tree. // 解析一个层次遍历
TreeNode* deserialize(string data) {
int len = data.size();
if (len <= 2)
return NULL;
int numsCount = 0;
vector nums;
string word = "";
for (int i = 1; i <= len - 2; i++){
if (data[i] == ',')
{
TreeNode* tmp = NULL;
if (word == "null")
{
}
else{
int num = atoi(word.c_str());
tmp = new TreeNode(num);
}
nums.push_back(tmp);
word = "";
} else{
//if (data[i] >= '0' && data[i] <= '9')
word += data[i];
}
}
if (word != "" && word != "null")
{
int num = atoi(word.c_str());
TreeNode* tmp = new TreeNode(num);
nums.push_back(tmp);
}
int cnt = nums.size();
int q = 0; // 遍历的节点,每个节点一个一个遍历(包括了NULL 节点)
int p = 1; // 当前遍历结点 的 左孩子节点, p+1就是右孩子节点
while (p < cnt)
{
if (nums[q] == NULL){
q++;
}
else{
if (p < cnt)
nums[q]->left = nums[p];
if (p + 1 < cnt)
nums[q]->right = nums[p + 1];
p += 2;
q++;
}
}
return nums[0];
}
};
// Your Codec object will be instantiated and called as such:
// Codec codec;
// codec.deserialize(codec.serialize(root));
参考其中的一个解题方法
http://blog.csdn.net/brucehb/article/details/50472732
char* Serialize(TreeNode *root) {
if (root == NULL)
return NULL;
queue sta;
sta.push(root);
string s = "";
char str[10];
while (!sta.empty())
{
while (!sta.empty())
{
TreeNode* tmp = sta.front();
sta.pop();
if (tmp != NULL)
{
sta.push(tmp->left);
sta.push(tmp->right);
sprintf(str, "%d", tmp->val);
s += str;
}
else
{
s += "#";
}
s += " ";
}
}// while
int end = s.length() - 1;
while (s[end] == '#' || s[end] == ' ')
end--;
char *rs = new char[end + 2];
//strcpy(rs, s.c_str());
for (int i = 0; i <= end; i++)
rs[i] = s[i];
rs[end + 1] = '\0';
return rs;
}
TreeNode* Deserialize(char *str) {
if (str == NULL)
return NULL;
int len = strlen(str);
if (len == 0)
return NULL;
int numsCount = 0;
vector nums;
string s(str);
stringstream ss(s);
string word;
while (ss >> word)
{
// cout << word << endl;
// nums.push_back(word);
if (word == "#") {
nums.push_back(NULL);
}
else{
int tmp = atoi(word.c_str());
nums.push_back(new TreeNode(tmp));
}
numsCount++;
}
int q = 0; // BFS层次,一个一个的处理节点
int p = 1; // 相当于q的左孩子
while (p < numsCount)
{
if (nums[q] == NULL){
q++;
}
else{
if (p < cnt)
nums[q]->left = nums[p];
if (p + 1 < cnt)
nums[q]->right = nums[p + 1];
p += 2;
q++;
}
}
TreeNode* root = nums[0];
return root;
}
参考:
http://www.nowcoder.com/practice/cf7e25aa97c04cc1a68c8f040e71fb84?tpId=13&tqId=11214&rp=4&ru=/ta/coding-interviews&qru=/ta/coding-interviews/question-ranking
http://www.nowcoder.com/questionTerminal/cf7e25aa97c04cc1a68c8f040e71fb84
/*
struct TreeNode {
int val;
struct TreeNode *left;
struct TreeNode *right;
TreeNode(int x) :
val(x), left(NULL), right(NULL) {
}
};
*/
class Solution {
public:
string seri(TreeNode * root)
{
if (root == NULL)
return "# ";
char str[11];
sprintf(str, "%d ", root->val);
string rs = str;
rs += seri(root->left);
rs += seri(root->right);
return rs;
}
char* Serialize(TreeNode *root) {
if (root == NULL)
return NULL;
string s = seri(root);
int len = s.size();
char *rs = new char[len+1];
//strcpy(rs, s.c_str());
for (int i = 0; i < len; i++)
rs[i] = s[i];
rs[len] = '\0';
return rs;
}
TreeNode* Deser(vector nums, int len ,int &index)
{
if (index >= len)
return NULL;
TreeNode* root = NULL;
if (index < len)
{
root = nums[index];
if (root != NULL){
root->left = Deser(nums, len, ++index);// 左边遇到null会到树底,返回后接着右子树,index始终++
root->right = Deser(nums, len, ++index);
}
}
return root;
}
TreeNode* Deserialize(char *str) {
if (str == NULL || (int)strlen(str) <= 0)
return NULL;
// 得到一个一个的前序遍历的节点
vector nums;
string s(str);
stringstream ss(s);
string word;
while (ss >> word)
{
// cout << word << endl;
// nums.push_back(word);
if (word == "#") {
nums.push_back(NULL);
}
else{
int tmp = atoi(word.c_str());
nums.push_back(new TreeNode(tmp));
}
}
int len = nums.size();
int index = 0;
TreeNode* root = Deser(nums,len, index); //解析前序遍历节点得到树
return root;
}
};
子树是一棵树的某个子树部分,可以采用序列化方法。
子结构是树的部分相同结构,不能采用序列化方法判断。