• 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 }
  • 相关阅读:
    详解 CSS 属性
    【技巧】DataGridView,ListView重新绑定时保持上次滚动位置
    c#自动更新+安装程序的制作 (转)
    C#winform程序安装时自动卸载新版本覆盖旧版本
    C# 操作XML文件,用XML文件保存信息
    c# winform 隐藏tabcontrol标签
    datagridview用get,set访问并加锁,可以控制所有使用datagridview的地方都顺序进行访问
    sql 获取当天开始时间 结束时间
    批量导出指定注册表内容
    sql server 保留小数,向上保留指定位数的小数,仅记录,勿看。
  • 原文地址:https://www.cnblogs.com/EdwardLiu/p/4391418.html
Copyright © 2020-2023  润新知