• [LeetCode] Serialize and Deserialize Binary Tree


    I adopt a way similar to that of the OJ to serialize the binary tree. For the following tree, my serialization result is "1,2,3,null,null,4,5," (note the last comma).

        1
       / 
      2   3
         / 
        4   5

    The serialization and deserialization both use the BFS traversal. The code is as follows.

    /**
     * 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) {
            vector<string> nodes;
            queue<TreeNode*> level;
            level.push(root);
            while (!level.empty()) {
                int n = level.size();
                for (int i = 0; i < n; i++) {
                    TreeNode* node = level.front(); level.pop();
                    nodes.push_back(node ? to_string(node->val) + "," : "null,");
                    if (node) {
                        level.push(node->left);
                        level.push(node->right);
                    }
                }
            }
            while (!nodes.empty() && nodes.back() == "null,")
                nodes.pop_back();
            string tree;
            for (string node : nodes) tree += node;
            return tree;
        }
    
        // Decodes your encoded data to tree.
        TreeNode* deserialize(string data) {
            if (data.empty()) return NULL;
            stringstream ss(data);
            string vs;
            getline(ss, vs, ',');
            TreeNode* root = new TreeNode(stoi(vs));
            queue<TreeNode*> level;
            level.push(root);
            while (!level.empty()) {
                int n = level.size();
                for (int i = 0; i < n; i++) {
                    TreeNode* node = level.front(); level.pop();
                    if (getline(ss, vs, ',') && vs != "null") {
                        node->left = new TreeNode(stoi(vs));
                        level.push(node -> left);
                    }
                    if (getline(ss, vs, ',') && vs != "null") {
                        node->right = new TreeNode(stoi(vs));
                        level.push(node -> right);
                    }
                }
            }
            return root;
        }
    };
    
    // Your Codec object will be instantiated and called as such:
    // Codec codec;
    // codec.deserialize(codec.serialize(root));
  • 相关阅读:
    获取连接无线路由客户机信息命令
    HTB进行流量控制方法
    exec函数族用法
    java.lang.RuntimeException: java.lang.reflect.InvocationTargetException
    struts2从请求取值的三种方式
    用jsp写的网页 怎么在传递参数时包含中文?
    Struts2使用DoubleSelect实现二级级联下拉框省份城市
    MySQL里主键与外键的关系
    查看struts2源码
    WIN7系统下,用笔记本发送WIFI信号让手机无线上网!
  • 原文地址:https://www.cnblogs.com/jcliBlogger/p/4923497.html
Copyright © 2020-2023  润新知