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 }
  • 相关阅读:
    opencv(8)直方图操作
    opencv(9)直方图均衡化,对比,匹配
    最近没有更新日记
    dsp 链接命令文件的写法
    sqlserver 的数据库备份 还原安全操作 备忘录
    主板的各种抱错声音
    hibernate 的自动生成工具
    如何学习,如何提出问题,如何解决问题,如何脑筋急转弯
    framework 的 错误提示?
    hack 入侵 142 主机的过程
  • 原文地址:https://www.cnblogs.com/EdwardLiu/p/4391418.html
Copyright © 2011-2022 走看看