码迷,mamicode.com
首页 > 其他好文 > 详细

Binary Tree Serialization

时间:2016-07-05 06:26:22      阅读:256      评论:0      收藏:0      [点我收藏+]

标签:

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.

分析:

使用inorder and preorder 来维护左子树,root,和右子树之间的关系。

 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         if (root == null) return "";
20         StringBuilder pre = new StringBuilder();
21         StringBuilder in = new StringBuilder();
22 
23         preOrder(root, pre);
24         inOrder(root, in);
25 
26         String all = pre.substring(0, pre.length() - 1) + "@" + in.substring(0, pre.length() - 1);
27         return all;
28     }
29 
30     public void preOrder(TreeNode root, StringBuilder pre) {
31         if (root != null) {
32             pre.append(root.val + ",");
33             preOrder(root.left, pre);
34             preOrder(root.right, pre);
35         }
36     }
37 
38     public void inOrder(TreeNode root, StringBuilder str) {
39         if (root != null) {
40             inOrder(root.left, str);
41             str.append(root.val + ",");
42             inOrder(root.right, str);
43         }
44     }
45 
46     /**
47      * This method will be invoked second, the argument data is what exactly you
48      * 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.equals("")) return null;
55         int index = data.indexOf("@");
56         String[] pre = data.substring(0, index).split(",");
57         String[] mid = data.substring(index + 1).split(",");
58         return createTree(pre, 0, mid, 0, pre.length);
59     }
60 
61     public TreeNode createTree(String[] preString, int preStart, String[] inString, int inStart, int length) {
62 
63         if (length <= 0) return null;
64 
65         TreeNode root = new TreeNode(Integer.parseInt(preString[preStart]));
66 
67         int index = findIndex(inString, root.val + "");
68         root.left = createTree(preString, preStart + 1, inString, inStart, index - inStart);
70         root.right = createTree(preString, preStart + (index - inStart) + 1, inString, index + 1, length - (index - inStart) - 1);
72 
73         return root;
74     }
75 
76     public int findIndex(String[] str, String val) {
77         for (int i = 0; i < str.length; i++) {
78             if (str[i].equals(val)) {
79                 return i;
80             }
81         }
82         return -1;
83     }
84 }

参考请注明出处:cnblogs.com/beiyeqingteng/

Binary Tree Serialization

标签:

原文地址:http://www.cnblogs.com/beiyeqingteng/p/5642226.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!