297. Serialize and Deserialize 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.
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.
分析:
方法1:
利用full tree的性质,拿到按层打印的string,然后同样按照每层还原。
1 /** 2 * Definition for a binary tree node. 3 * public class TreeNode { 4 * int val; 5 * TreeNode left; 6 * TreeNode right; 7 * TreeNode(int x) { val = x; } 8 * } 9 */ 10 public class Codec { 11 private static final String spliter = ","; 12 private static final String NN = "X"; 13 14 public String serialize(TreeNode root) { 15 if (root == null) return ""; 16 Queue<TreeNode> q = new LinkedList<>(); 17 StringBuilder res = new StringBuilder(); 18 q.add(root); 19 while (!q.isEmpty()) { 20 TreeNode node = q.poll(); 21 if (node == null) { 22 res.append(NN).append(spliter); 23 } else { 24 res.append(node.val).append(spliter); 25 q.add(node.left); 26 q.add(node.right); 27 } 28 } 29 return res.toString(); 30 } 31 32 public TreeNode deserialize(String data) { 33 if (data == "") return null; 34 Queue<TreeNode> q = new LinkedList<>(); 35 String[] values = data.split(spliter); 36 TreeNode root = new TreeNode(Integer.parseInt(values[0])); 37 q.add(root); 38 int i = 1; 39 while (!q.isEmpty()) { 40 TreeNode parent = q.poll(); 41 if (!values[i].equals(NN)) { 42 TreeNode left = new TreeNode(Integer.parseInt(values[i])); 43 parent.left = left; 44 q.add(left); 45 } 46 i++; 47 if (!values[i].equals(NN)) { 48 TreeNode right = new TreeNode(Integer.parseInt(values[i])); 49 parent.right = right; 50 q.add(right); 51 } 52 i++; 53 } 54 return root; 55 } 56 } 57 58 // Your Codec object will be instantiated and called as such: 59 // Codec ser = new Codec(); 60 // Codec deser = new Codec(); 61 // TreeNode ans = deser.deserialize(ser.serialize(root));
利用preorder + full tree的性质, 我们可以把bt还原。对于一个full tree, 当我们用recursion 还原完成左边部分后,剩余部分永远是属于右边部分的。
1 public class Codec { 2 private static final String spliter = ","; 3 private static final String NN = "X"; 4 5 public String serialize(TreeNode root) { 6 StringBuilder sb = new StringBuilder(); 7 buildString(root, sb); 8 return sb.toString(); 9 } 10 11 private void buildString(TreeNode node, StringBuilder sb) { 12 if (node == null) { 13 sb.append(NN).append(spliter); 14 } else { 15 sb.append(node.val).append(spliter); 16 buildString(node.left, sb); 17 buildString(node.right,sb); 18 } 19 } 20 // Decodes your encoded data to tree. 21 public TreeNode deserialize(String data) { 22 Queue<String> nodes = new LinkedList<>(); 23 nodes.addAll(Arrays.asList(data.split(spliter))); 24 return buildTree(nodes); 25 } 26 27 private TreeNode buildTree(Queue<String> nodes) { 28 String val = nodes.remove(); 29 if (val.equals(NN)) { 30 return null; 31 } else { 32 TreeNode node = new TreeNode(Integer.valueOf(val)); 33 node.left = buildTree(nodes); 34 node.right = buildTree(nodes); 35 return node; 36 } 37 } 38 }

浙公网安备 33010602011771号