Implement two functions are used to serialize and deserialize binary
It refers to a sequence of binary: the binary tree is saved in a format string in accordance with the result of a traversing manner such that the memory can be set up binary persisted. Serialization may be based on the first order, in sequence, after, the binary tree traversal sequence is modified, the sequence of the result is a string representing the blank nodes (#) by some sequences of symbol to! End node represents a value (value!).
Deserialized binary tree means: The serialized string str some results obtained traversal order to reconstruct a binary tree.
Solution: recursive
. 1 public static String the Serialize (the TreeNode the root) { 2 // preorder . 3 IF (the root == null ) { . 4 return "#_" ; . 5 } . 6 String root.val = RES + "_" ; . 7 RES + = the Serialize (root.left); . 8 RES + = the serialize (root.right); . 9 return RES; 10 } . 11 / ** 12 is * a serialized string loaded in a queue 13 is * / 14 public staticThe Deserialize the TreeNode (String STR) { 15 Queue <String> Queue = new new the LinkedList <> (); 16 Collections.addAll (Queue, str.split ( "_" )); . 17 return the Deserialize (Queue); 18 is } . 19 / * * 20 * preorder traversal deserialization 21 * dequeues a judgment is "#", if it is indicating that the node is null, or the new node 22 is * / 23 is public static the TreeNode the deserialize (queue <String> queue ) { 24 String String = queue.poll (); 25 IF (String.equals ( "#" )) { 26 is return null; 27 } 28 TreeNode root = new TreeNode(Integer.parseInt(string)); 29 root.left = Deserialize(queue); 30 root.right = Deserialize(queue); 31 return root; 32 }
Initialization tree:
1 public static class TreeNode{ 2 int val=0; 3 TreeNode left=null; 4 TreeNode right=null; 5 public TreeNode(int val){ 6 this.val=val; 7 } 8 } 9 private static List<TreeNode> nodeList = null; 10 public static TreeNode createBinTree(int[] array) { 11 = the nodeList new new the LinkedList <TreeNode> (); 12 is // the value in the array in turn converted to TreeNode node 13 is for ( int nodeIndex = 0; nodeIndex <be array.length; nodeIndex ++ ) { 14 nodeList.add ( new new TreeNode (Array [ nodeIndex])); 15 } 16 // establish lastParentIndex-1 before the parent of node a digital relationship with the child node of the parent node of a binary tree . 17 for ( int parentIndex = 0; parentIndex <be array.length / 2 -. 1; parentIndex ++ ) { 18 // left child 19 nodeList.get (parentIndex) .left = nodeList 20 is .get (parentIndex. 1 * 2 + ); 21 is // right child 22 is nodeList.get (parentIndex) .right = the nodeList 23 is .get (parentIndex * 2 + 2 ); 24 } 25 // last parent node: as the final a parent node may not be the right child, so separate out handle 26 is int lastParentIndex be array.length = / 2 -. 1 ; 27 // left child 28 nodeList.get (lastParentIndex) .left = the nodeList 29 .get (lastParentIndex. 1 + 2 * ); 30 // right child, if the length of the array is odd only establish the right child 31 if (array.length % 2 == 1) { 32 nodeList.get(lastParentIndex).right = nodeList 33 .get(lastParentIndex * 2 + 2); 34 } 35 return nodeList.get(0); 36 }
Print traversal sequence:
1 public static ArrayList<ArrayList<Integer>> Print(TreeNode pRoot) { 2 ArrayList<ArrayList<Integer>> res = new ArrayList<>(); 3 LinkedList<TreeNode> queue = new LinkedList<>(); 4 if(pRoot==null){ 5 return res; 6 } 7 queue.offer(pRoot); 8 while (queue.size()!=0){ 9 int size=queue.size(); 10 ArrayList<Integer> list = new ArrayList<>(); 11 for(int i=0;i<size;i++) { 12 TreeNode node = queue.poll(); 13 if(node==null){ 14 continue; 15 } 16 list.add(node.val); 17 queue.offer(node.left); 18 queue.offer(node.right); 19 } 20 if(list.size()!=0){ 21 res.add(list); 22 } 23 } 24 return res; 25 }
test:
1 public static void main(String[] args) { 2 int[] tree={8,6,10,5,7,9,11}; 3 TreeNode root = createBinTree(tree); 4 String serialize = Serialize(root); 5 System.out.println(serialize); 6 TreeNode deserialize = Deserialize(serialize); 7 ArrayList<ArrayList<Integer>> lists = Print(deserialize); 8 for (ArrayList<Integer> list : lists) { 9 System.out.println(list); 10 } 11 } 12 Output: 13 8_6_5 _ 7 _ # _ # _ # _ # _ # _ # _ 10_9 _ # _ # _ 11 _ 14 [8 ] 15 [6, 10 ] 16 [5, 7, 9, 11]