zoukankan      html  css  js  c++  java
  • Lintcode: Binary Tree Serialization (Serialization and Deserialization Of Binary Tree)

    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.
    
    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.

    Serialization 和 Deserialization都是用BFS, Serialization注意要删除String末尾多余的“#”, Deserialization维护一个count指示当前TreeNode对应的值

     1 class Solution {
     2     /**
     3      * This method will be invoked first, you should design your own algorithm 
     4      * to serialize a binary tree which denote by a root node to a string which
     5      * can be easily deserialized by your own "deserialize" method later.
     6      */
     7     public String serialize(TreeNode root) {
     8         // write your code here
     9         StringBuffer res = new StringBuffer();
    10         if (root == null) return res.toString();
    11         LinkedList<TreeNode> queue = new LinkedList<TreeNode>();
    12         queue.offer(root);
    13         res.append(root.val);
    14         while (!queue.isEmpty()) {
    15             TreeNode cur = queue.poll();
    16             if (cur.left != null)   queue.offer(cur.left); //add children to the queue
    17             if (cur.right != null)  queue.offer(cur.right);
    18             res.append(",");
    19             if (cur.left != null) {
    20                 res.append(cur.left.val);
    21             }
    22             else res.append("#");
    23             res.append(",");
    24             if (cur.right != null) {
    25                 res.append(cur.right.val);
    26             }
    27             else res.append("#");
    28         }
    29         int i = res.length()-1;
    30         while (i>=0 && res.charAt(i)=='#') {
    31             res.deleteCharAt(i);
    32             res.deleteCharAt(i-1);
    33             i -= 2;
    34         }
    35         return res.toString();
    36     }
    37     
    38     /**
    39      * This method will be invoked second, the argument data is what exactly
    40      * you serialized at method "serialize", that means the data is not given by
    41      * system, it's given by your own serialize method. So the format of data is
    42      * designed by yourself, and deserialize it here as you serialize it in 
    43      * "serialize" method.
    44      */
    45     public TreeNode deserialize(String data) {
    46         // write your code here
    47         if (data==null || data.length()==0) return null;
    48         String[] arr = data.split(",");
    49         int len = arr.length;
    50         int count = 0;
    51         TreeNode root = new TreeNode(Integer.parseInt(arr[0]));
    52         LinkedList<TreeNode> queue = new LinkedList<TreeNode>();
    53         queue.offer(root);
    54         count++;
    55         while (!queue.isEmpty()) {
    56             TreeNode cur = queue.poll();
    57             String left="", right="";
    58             if (count < len) {
    59                 left = arr[count];
    60                 count++;
    61                 if (!left.equals("#")) {
    62                     cur.left = new TreeNode(Integer.parseInt(left));
    63                     queue.offer(cur.left);
    64                 }
    65                 else cur.left = null;
    66             }
    67             else cur.left = null;
    68             
    69             if (count < len) {
    70                 right = arr[count];
    71                 count++;
    72                 if (!right.equals("#")) {
    73                     cur.right = new TreeNode(Integer.parseInt(right));
    74                     queue.offer(cur.right);
    75                 }
    76                 else cur.right = null;
    77             }
    78             else cur.right = null;
    79         }
    80         return root;
    81     }
    82 }
  • 相关阅读:
    微信小程序学习心得
    微信小程序分类的实现
    Vue实例中封装api接口的思路 在页面中用async,await调用方法请求
    Vue中封装axios组件实例
    使用creata-react-app脚手架创建react项目时非常慢的问题
    Javascript的对象
    vue中上拉加载数据的实现
    Vue中键盘事件
    vant学习网址
    字符串,字典,数组写入本地文件和从本地文件读取
  • 原文地址:https://www.cnblogs.com/EdwardLiu/p/4391418.html
Copyright © 2011-2022 走看看