序列化二叉树

Posted chengsheng

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了序列化二叉树相关的知识,希望对你有一定的参考价值。

题目描述

请实现两个函数,分别用来序列化和反序列化二叉树
 
解题思路:
利用层次遍历的思想。序列化时,节点值用“ ”空格隔开,空节点使用“#”代替。
反序列化时把提取出来的节点放入队列中,每次循环都处理的是该节点的左右儿子节点。
class Solution {
public:

    char* Serialize(TreeNode *root) {
        string res;
        if(root == NULL) return NULL;
        queue<TreeNode*> que;
        TreeNode * tmpNode = NULL;
        que.push(root);
        res += tostring(root->val) + " ";
        while(!que.empty()){
            tmpNode = que.front();
            que.pop();
            if(tmpNode->left != NULL){
                que.push(tmpNode->left);
                res+= tostring(tmpNode->left->val) + " ";
            }else{
                res += "# ";
            }
            if(tmpNode->right != NULL){
                que.push(tmpNode->right);
                res+= tostring(tmpNode->right->val) + " ";
            }else{
                res += "# ";
            }
        }
        int len = res.size();
        char *ret = new char[len+1];
        strcpy(ret, res.c_str());
        return ret;
    }
    TreeNode* Deserialize(char *str) {
        int i = 0;
        if(str == NULL) return NULL;
        if(str[0] == ‘‘) return NULL;
        int num = pickNum(str, i);
        queue<TreeNode*> que;
        TreeNode *ret = new TreeNode(num);
        TreeNode* tmpNode = NULL;
        que.push(ret);
        while(!que.empty()){
            tmpNode = que.front();
            que.pop();
            cout<<"tmpNode->val="<<tmpNode->val<<endl;
            cout<<"str[i]="<<str[i]<<endl;
            if(str[i] >= ‘0‘ && str[i] <= ‘9‘){
                int lv = pickNum(str, i);
                //cout<<"lv="<<lv<<endl;
                tmpNode->left = new TreeNode(lv);
                que.push(tmpNode->left);
            }else{
                if(str[i] == ‘#‘){
                    i+=2;
                }
            }
            if(str[i] >= ‘0‘ && str[i] <= ‘9‘){
                int rv = pickNum(str, i);
                //cout<<"rv="<<rv<<endl;
                tmpNode->right = new TreeNode(rv);
                que.push(tmpNode->right);
            }else{
                if(str[i] == ‘#‘){
                    i+=2;
                }
            }
        }
        return ret;
    }
   //打印二叉树的层次结构
   vector<vector<int> > Print(TreeNode* pRoot) {
            vector<vector<int> > res;
            if(pRoot == NULL) return res;
            queue<TreeNode *> que1, que2;
            que1.push(pRoot);
            res.push_back(vector<int>(1, pRoot->val));
            TreeNode* tmpNode=NULL;
            while(!que1.empty() || !que2.empty()){
                vector<int>tmpVct;
                while(!que1.empty()){
                    tmpNode = que1.front();
                    que1.pop();
                    if(tmpNode->left != NULL){
                        que2.push(tmpNode->left);
                        tmpVct.push_back(tmpNode->left->val);
                    }
                    if(tmpNode->right != NULL){
                        que2.push(tmpNode->right);
                        tmpVct.push_back(tmpNode->right->val);
                    }
                }
                if(tmpVct.size() != 0)
                    res.push_back(tmpVct);
                tmpVct.clear();
                while(!que2.empty()){
                    tmpNode = que2.front();
                    que2.pop();
                    if(tmpNode->left != NULL){
                        que1.push(tmpNode->left);
                        tmpVct.push_back(tmpNode->left->val);
                    }
                    if(tmpNode->right != NULL){
                        que1.push(tmpNode->right);
                        tmpVct.push_back(tmpNode->right->val);
                    }
                }
                if(tmpVct.size() != 0)
                    res.push_back(tmpVct);
            }
            return res;
        }
private:
    string tostring(int a){
        strstream ss;
        ss<<a;
        string res;
        ss>>res;
        return res;
    }
    int pickNum(char *str, int & i){
        int res = 0;
        while(str[i]>=‘0‘ && str[i] <= ‘9‘){
            res = res*10 + str[i]-‘0‘;
            i++;
        }
        if(str[i] == ‘ ‘) i++;
        return res;
    }
};

  

以上是关于序列化二叉树的主要内容,如果未能解决你的问题,请参考以下文章

Offer[37] 序列化二叉树

编程实现以上二叉树中序遍历操作,输出遍历序列,求写代码~~

63.序列化二叉树

与二叉树有关的编程题的Java代码实现

50.序列化二叉树

剑指OFFER 序列化二叉树