leetcode 297 : Serialize and Deserialize Binary Tree

来源:互联网 发布:淘宝达人如何大v认证 编辑:程序博客网 时间:2024/05/21 17:57

1、原题如下:
Serialization is the process of converting a data structure or object into a sequence of bits so that it can be stored in a file or memory buffer, or transmitted across a network connection link to be reconstructed later in the same or another computer environment.

Design an algorithm to serialize and deserialize a binary tree. There is no restriction on how your serialization/deserialization algorithm should work. You just need to ensure that a binary tree can be serialized to a string and this string can be deserialized to the original tree structure.

For example, you may serialize the following tree

1
/ \
2 3
/ \
4 5
as “[1,2,3,null,null,4,5]”, just the same as how LeetCode OJ serializes a binary tree. You do not necessarily need to follow this format, so please be creative and come up with different approaches yourself.
Note: Do not use class member/global/static variables to store states. Your serialize and deserialize algorithms should be stateless.

2、解题如下:

/** * 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) {        queue<TreeNode*> bt;        bt.push(root);        string result;        while(!bt.empty())        {            if(bt.front()==nullptr)                result=result+"#,";            else            {                bt.push(bt.front()->left);                bt.push(bt.front()->right);                result=result+to_string(bt.front()->val)+",";            }            bt.pop();        }        return result;    }    // Decodes your encoded data to tree.    TreeNode* deserialize(string data) {        queue<TreeNode**> bt2;        TreeNode* root=nullptr;        bt2.push(&root);        string::iterator one=data.begin();        while(one!=data.end())        {            TreeNode** temp=bt2.front();            if(*one=='#')            {                *temp=nullptr;                advance(one,2);            }            else            {                string ::iterator two=find(one,data.end(),',');                int value=stoi(string(one,two));                *temp=new TreeNode(value);                bt2.push(&((*temp)->left));                bt2.push(&((*temp)->right));                one=next(two);            }            bt2.pop();        }        return root;    }};// Your Codec object will be instantiated and called as such:// Codec codec;// codec.deserialize(codec.serialize(root));

3、总结
(1)queue的成员函数:
empty()判断队列空,当队列空时,返回true。
size()访问队列中的元素个数。
push()会将一个元素置入queue中。
front()会返回queue内的第一个元素(也就是第一个被置入的元素)。
back()会返回queue中最后一个元素(也就是最后被插入的元素)。
pop()会从queue中移除一个元素。[1]
注意:pop()虽然会移除下一个元素,但是并不返回它,front()和back()返回下一个元素但并不移除该元素。

(2)advance
Advance(i, n) increments the iterator i by the distance n. If n > 0 it is equivalent to executing ++i n times, and if n < 0 it is equivalent to executing –i n times. If n == 0, the call has no effect.
advance(i, n)使得迭代器i增加一个长度n。如果n>0,那么advance(i, n)等价于执行++i操作n次,如果n<0,那么等价于执行- -i操作n次,如果n=0,那么这个调用没有任何影响。

0 0
原创粉丝点击