zoukankan      html  css  js  c++  java
  • LintCode-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.

    Solution:

     1 /**
     2  * Definition of TreeNode:
     3  * public class TreeNode {
     4  *     public int val;
     5  *     public TreeNode left, right;
     6  *     public TreeNode(int val) {
     7  *         this.val = val;
     8  *         this.left = this.right = null;
     9  *     }
    10  * }
    11  */
    12 class Solution {
    13     /**
    14      * This method will be invoked first, you should design your own algorithm 
    15      * to serialize a binary tree which denote by a root node to a string which
    16      * can be easily deserialized by your own "deserialize" method later.
    17      */
    18     public String serialize(TreeNode root) {
    19         String res = "";
    20         if (root==null) return res;
    21         
    22         List<TreeNode> nodeList = new ArrayList<TreeNode>();
    23         nodeList.add(root);
    24         int index = 0;
    25         res = Integer.toString(nodeList.get(0).val);
    26         while (index<nodeList.size()){
    27             TreeNode cur = nodeList.get(index);
    28             if (cur==null){
    29                 res += ",#,#";
    30             } else {
    31                 if (cur.left!=null){
    32                     nodeList.add(cur.left);
    33                     res += ","+cur.left.val;
    34                 } else res += ",#";
    35                 if (cur.right!=null){
    36                     nodeList.add(cur.right);
    37                     res += ","+cur.right.val;
    38                 } else res += ",#";
    39             }
    40             index++;
    41         }       
    42         
    43         return res;
    44     }
    45     
    46     /**
    47      * This method will be invoked second, the argument data is what exactly
    48      * you serialized at method "serialize", that means the data is not given by
    49      * system, it's given by your own serialize method. So the format of data is
    50      * designed by yourself, and deserialize it here as you serialize it in 
    51      * "serialize" method.
    52      */
    53     public TreeNode deserialize(String data) {
    54         if (data.isEmpty()) return null;
    55         String[] temp = data.split(",");
    56         TreeNode root = new TreeNode(Integer.parseInt(temp[0]));
    57         List<TreeNode> nodeList = new ArrayList<TreeNode>();
    58         nodeList.add(root);
    59         int cur = 0, next = 1;
    60         while (cur<nodeList.size()){
    61             TreeNode curNode = nodeList.get(cur);
    62             if (curNode==null) cur++;
    63             else {           
    64                 int left = next;
    65                 int right = next+1;
    66                 if (next<temp.length){
    67                     if (temp[next].equals("#")) nodeList.add(null);
    68                     else {
    69                         TreeNode leftChild = new TreeNode(Integer.parseInt(temp[next]));
    70                         curNode.left = leftChild;
    71                         nodeList.add(leftChild);
    72                     }
    73                 }
    74                 if (next+1<temp.length){
    75                     if (temp[next+1].equals("#")) nodeList.add(null);
    76                     else {
    77                         TreeNode rightChild = new TreeNode(Integer.parseInt(temp[next+1]));
    78                         curNode.right = rightChild;
    79                         nodeList.add(rightChild);
    80                     }
    81                 }
    82                 next += 2;
    83                 cur++;
    84             }
    85         }
    86         
    87         
    88         
    89         return root;    
    90     }
    91 }
  • 相关阅读:
    记录:将图片数据生成 tfrecords 文件并在训练使用时读取
    记录:EM 算法估计混合高斯模型参数
    记录:Ubuntu 18.04 安装 tensorflow-gpu 版本
    记录:tf.saved_model 模块的简单使用(TensorFlow 模型存储与恢复)
    记录:TensorFlow 中的 padding 方式
    mybatis list映射
    idea使用插件mybatis-generator-plus生成mapper文件(mysql亲测可用)
    Element-UI树形表格
    Mysql5.7版本ERROR 1055问题
    为DISTINCT、GROUP BY和ORDER BY效率问题提提速
  • 原文地址:https://www.cnblogs.com/lishiblog/p/4210573.html
Copyright © 2011-2022 走看看