zoukankan      html  css  js  c++  java
  • LintCode Binary Tree Serialization

    Design an algorithm and write code to serialize and deserialize a binary tree. Writing the tree to a file is called 'serialization' and reading back from the file to reconstruct the exact same binary tree is 'deserialization'.
    There is no limit of how you deserialize or serialize a binary tree, you only need to make sure you can serialize a binary tree to a string and deserialize this string to the original structure.
    Have you met this question in a real interview? Yes
    Example
    An example of testdata: Binary tree {3,9,20,#,#,15,7}, denote the following structure:

      3
     / 
    9  20
      /  
     15   7
    

    Our data serialization use bfs traversal. This is just for when you got wrong answer and want to debug the input.
    You can use other method to do serializaiton and deserialization.

    /**
     * Definition of TreeNode:
     * class TreeNode {
     * public:
     *     int val;
     *     TreeNode *left, *right;
     *     TreeNode(int val) {
     *         this->val = val;
     *         this->left = this->right = 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) {
            // write your code here
            string res;
            serdfs(root, res);
            return res;
        }
        
        void serdfs(TreeNode* root, string& output) {
            if (root == NULL) {
                output.append("#,");
                return;
            }
            output.append(to_string(root->val)+",");
            
            serdfs(root->left, output);
            serdfs(root->right, output);
        }
    
        /**
         * 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) {
            // write your code here
            int pos = 0;
            return derdfs(data, pos);
        }
        
        TreeNode* derdfs(string& input, int& pos) {
            if (pos >= input.size() || input[pos] == '#') {
                pos++;
                pos++;
                return NULL;
            }
            int val = 0;
            int len = input.size();
            
            while (pos < len && input[pos] >= '0' && input[pos] <= '9') {
                val = val * 10 + input[pos++] - '0';
            }
            pos++;
            TreeNode* root = new TreeNode(val);
            root->left = derdfs(input, pos);
            root->right= derdfs(input, pos);
            
            return root;
        }
    };
    

    前序遍历,保留NULL值(用#表示),节点之间采用','隔开

  • 相关阅读:
    【已解决】Kettle新建数据库连接报错(Mysql,MS Sql Server)
    SQL面试题-练习2
    WIN7bat批处理遍历文件夹,输出当前文件夹下所有文件。
    【已解决】MYSQL安装过程报错,怎么解决?MySQL error 0: Authentication to host 'localhost' for user 'root' using method 'caching_sha2_password' failed with message: Reading from the stream has failed.
    常用外国在线英语词典-单词查询
    Oracle 11g 服务端的安装步骤
    Oracle 查询(SELECT)语句(一)
    Oracle 增删改(INSERT、DELETE、UPDATE)语句
    记录一个 C# 导出 Excel 的坑
    C# 中的浅拷贝与深拷贝
  • 原文地址:https://www.cnblogs.com/lailailai/p/4832753.html
Copyright © 2011-2022 走看看