• 二叉树的序列化和反序列化


    /**
     * Definition of TreeNode:
     * class TreeNode {
     * public:
     *     int val;
     *     TreeNode *left, *right;
     *     TreeNode(int val) {
     *         this->val = val;
     *         this->left = this->right = NULL;
     *     }
     * }
     */
    
    // 方法1:递归写法
    // 一个思路是根据{先序遍历,中序遍历}或者{中序遍历,后续遍历}可以构造二叉树,但是重建的
    // 时候有个条件:没有重复的元素。下面的递归方法一次先序遍历就可以重建出二叉树,原因是牺牲
    // 了一定的存储空间,即叶子节点的两个NULL值也要保存起来,才能正常解析。
    class Solution {
    public:
        /**
         * This method will be invoked first, you should design your own algorithm 
         * to serialize a binary tree which denote by a root node to a string which
         * can be easily deserialized by your own "deserialize" method later.
         */
        string serialize(TreeNode * root) {
            std::ostringstream oss;
            Serialize(root, oss);
            return oss.str();
        }
    
        /**
         * This method will be invoked second, the argument data is what exactly
         * you serialized at method "serialize", that means the data is not given by
         * system, it's given by your own serialize method. So the format of data is
         * designed by yourself, and deserialize it here as you serialize it in 
         * "serialize" method.
         */
        TreeNode * deserialize(string &data) {
            std::istringstream iss(data);
            return Deserialize(iss);
        }
    
    private:
        void Serialize(TreeNode* root, std::ostringstream& oss) {
            if (root == NULL) {
                oss << "# ";
                return;
            }
            oss << root->val << " ";
            Serialize(root->left, oss);
            Serialize(root->right, oss);
        }
        
        TreeNode* Deserialize(std::istringstream& iss) {
            string val;
            iss >> val;
            if (val == "#") {
                return NULL;
            }
            TreeNode* root = new TreeNode(std::stoi(val));
            root->left = Deserialize(iss);
            root->right = Deserialize(iss);
            return root;
        }
    };
    
    
    // 方法2:非递归写法
    // 序列化和反序列化都是按层序遍历来做
    class Solution {
    public:
        /**
         * This method will be invoked first, you should design your own algorithm 
         * to serialize a binary tree which denote by a root node to a string which
         * can be easily deserialized by your own "deserialize" method later.
         */
        string serialize(TreeNode * root) {
            std::ostringstream oss;
            Serialize(root, oss);
            return oss.str();
        }
    
        /**
         * This method will be invoked second, the argument data is what exactly
         * you serialized at method "serialize", that means the data is not given by
         * system, it's given by your own serialize method. So the format of data is
         * designed by yourself, and deserialize it here as you serialize it in 
         * "serialize" method.
         */
        TreeNode * deserialize(string &data) {
            std::istringstream iss(data);
            return Deserialize(iss);
        }
    
    private:
        void Serialize(TreeNode* root, std::ostringstream& oss) {
            queue<TreeNode*> q;
            q.push(root);
            while (!q.empty()) {
                TreeNode* cur = q.front(); q.pop();
                if (cur == NULL) {
                    oss << "# ";
                } else {
                    oss << cur->val << " ";
                    q.push(cur->left);
                    q.push(cur->right);
                }
            }
        }
        
        TreeNode* Deserialize(std::istringstream& iss) {
            string val;
            iss >> val;
            if (val == "#") {
                return NULL;
            }
            
            TreeNode* root = new TreeNode(std::stoi(val));
            
            queue<TreeNode*> q;
            q.push(root);
            while (!q.empty()) {
                TreeNode* cur = q.front(); q.pop();
                // create left child node
                iss >> val;
                if (val == "#") {
                    cur->left = NULL;
                } else {
                    TreeNode* lc = new TreeNode(std::stoi(val));
                    cur->left = lc;
                    q.push(lc);
                }
                // create right child node
                iss >> val;
                if (val == "#") {
                    cur->right = NULL;
                } else {
                    TreeNode* rc = new TreeNode(std::stoi(val));
                    cur->right = rc;
                    q.push(rc);
                }
            }
            return root;
        }
    };
    
  • 相关阅读:
    MFC Slider控件 去掉边上的虚线
    VC學習網址
    全局程序集缓存工具 (Gacutil.exe)
    滚动条集合
    调用 DialogBox 会失败解决方法
    全局程序集缓存GAC”是什么概念
    UltraVNC:超实用的远程控制工具(图)
    VC程序员之无法选择的命运
    C++类
    角色权限批量设置,随点!
  • 原文地址:https://www.cnblogs.com/ilovezyg/p/7593756.html
Copyright © 2020-2023  润新知