zoukankan      html  css  js  c++  java
  • Leetcode 449. Serialize and Deserialize BST

    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 search tree. There is no restriction on how your serialization/deserialization algorithm should work. You just need to ensure that a binary search tree can be serialized to a string and this string can be deserialized to the original tree structure.

    The encoded string should be as compact as possible.

    Note: Do not use class member/global/static variables to store states. Your serialize and deserialize algorithms should be stateless.

    解题思路:编码的过程是将二叉排序树的节点int值转换成string,组成一个长字符串;解码过程是将这个长字符串分解成int数值然后去进行构建二叉查找(排序)树。

      1 #include <stdio.h>
      2 
      3 
      4 struct TreeNode {
      5     int val;
      6     TreeNode *left;
      7     TreeNode *right;
      8     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
      9 };
     10 
     11 #include <string>
     12 #include <vector>
     13 
     14 void BST_insert(TreeNode *node, TreeNode *insert_node){
     15     if (insert_node->val < node->val){
     16         if (node->left){
     17             BST_insert(node->left, insert_node);
     18         }
     19         else{
     20             node->left = insert_node;
     21         }
     22     }
     23     else{
     24         if (node->right){
     25             BST_insert(node->right, insert_node);
     26         }
     27         else{
     28             node->right = insert_node;
     29         }
     30     }
     31 }
     32 
     33 void change_int_to_string(int val, std::string &str_val){
     34     std::string tmp;
     35     while(val){
     36         tmp += val % 10 + '0';
     37         val = val / 10;
     38     }
     39     for (int i = tmp.length() - 1; i >= 0; i--){
     40         str_val += tmp[i];
     41     }
     42     str_val += '#';
     43 }
     44 
     45 void BST_preorder(TreeNode *node, std::string &data){
     46     if (!node){
     47         return;
     48     }
     49     std::string str_val;
     50     change_int_to_string(node->val, str_val);
     51     data += str_val;
     52     BST_preorder(node->left, data);
     53     BST_preorder(node->right, data);
     54 }
     55 
     56 class Codec {
     57 public:
     58     std::string serialize(TreeNode* root) {
     59         std::string data;
     60         BST_preorder(root, data);
     61         return data;
     62     }
     63     TreeNode *deserialize(std::string data) {
     64         if (data.length() == 0){
     65             return NULL;
     66         }
     67         std::vector<TreeNode *> node_vec;
     68         int val = 0;
     69         for (int i = 0; i < data.length(); i++){
     70             if (data[i] == '#'){
     71                 node_vec.push_back(new TreeNode(val));
     72                 val = 0;
     73             }
     74             else{
     75                 val = val * 10 + data[i] - '0';
     76             }
     77         }
     78         for (int i = 1; i < node_vec.size(); i++){
     79             BST_insert(node_vec[0], node_vec[i]);
     80         }
     81         return node_vec[0];
     82     }
     83 };
     84 
     85 void preorder_print(TreeNode *node,int layer){
     86     if (!node){
     87         return;
     88     }
     89     for (int i = 0; i < layer; i++){
     90         printf("-----");
     91     }
     92     printf("[%d]
    ", node->val);
     93     preorder_print(node->left, layer + 1);
     94     preorder_print(node->right, layer + 1);
     95 }
     96 
     97 int main(){
     98     TreeNode a(8);
     99     TreeNode b(3);
    100     TreeNode c(10);
    101     TreeNode d(1);
    102     TreeNode e(6);
    103     TreeNode f(15);    
    104     a.left = &b;
    105     a.right = &c;
    106     b.left = &d;
    107     b.right = &e;
    108     c.left = &f;    
    109     Codec solve;    
    110     std::string data = solve.serialize(&a);
    111     printf("%s
    ", data.c_str());
    112     TreeNode *root = solve.deserialize(data);
    113     preorder_print(root, 0);    
    114     return 0;
    115 }

     

  • 相关阅读:
    第一册:lesson 117.
    第一册:lesson 115.
    Map集合。
    第一册:lesson 113.
    第一册:lesson 111.
    泛型。
    EXT.NET初学
    LINQ查询
    后台调用前端JS
    数字与数组或者字符串里面的内容比较
  • 原文地址:https://www.cnblogs.com/Hwangzhiyoung/p/8733551.html
Copyright © 2011-2022 走看看