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 }
  • 相关阅读:
    读后感
    每日总结
    融e学 一个专注于重构知识,培养复合型人才的平台【获取考试答案_破解】
    营销经验总结:如何才能提升h5游戏代入感?
    在通过《令人心动的offer》中,掌握学到的公司选择企业邮箱3大技巧!
    2020年的最后一个月来了!TOM 企业邮箱陪伴您度过!
    用户主动分享h5游戏的4大理由
    企业邮箱的作用是什么?企业邮箱有什么用处?
    电子邮箱系统哪家好?邮箱登陆入口是?
    专业外贸企业邮箱,企业邮箱退信怎么办?
  • 原文地址:https://www.cnblogs.com/EdwardLiu/p/4391418.html
Copyright © 2011-2022 走看看