标签:
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.
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 }
LintCode-Serialization and Deserialization Of Binary Tree
标签:
原文地址:http://www.cnblogs.com/lishiblog/p/4210573.html