How a structure is preserved
Understand
For example, this tree is traversed in order first. No child is replaced by ා, and each node is separated by,
1_2_4_###3_5##6##_
So this process saves the entire tree structure
The reconstruction of this tree is deserialization
1 must be the head node, then 2 is the left child, 4 is the left child of 2, and Chen indicates that 4 is the leaf node. At this time, go back to 2, Chen indicates that 2 has no right child, then go to 3, go left to 5, Chen indicates that 5 has no child, go to 3, 3 to the right, go to 6, Chen indicates that 6 has no child, and finish
Deserialize as you serialize
Preorder
//Preorder traversal serialization public static String serialByPre(Node head) { if (head == null) { return "#!"; } String res = head.value + "!"; //Around now res += serialByPre(head.left); res += serialByPre(head.right); return res; } //De serialization public static Node reconByPreString(String preStr) { String[] value = preStr.split("!"); //Cut! Queue<String> queue = new LinkedList<String>(); for (int i = 0; i != value.length; i++) { //Queue in turn queue.offer(value[i]); } return reconPreOrder(queue); } private static Node reconPreOrder(Queue<String> queue) { String value = queue.poll(); if (value.equals("#") {/ / empty node return null; } Node head = new Node(Integer.valueOf(value)); //Consumption value reconstruction node head.left = reconPreOrder(queue); head.right = reconPreOrder(queue); return head; }
arrangement
//arrangement public static String serialBylevel(Node head) { if (head == null) { return "#!"; } String res = head.value + "!"; Queue<Node> queue = new LinkedList<Node>(); queue.offer(head); while (!queue.isEmpty()) { //Put them in the queue if they have children and continue to operate on them. Return if they have no children head = queue.poll(); if (head.left != null) { res += head.left.value + "!"; queue.offer(head.left); } else { res += "#!"; } if (head.right != null) { res += head.right.value + "!"; queue.offer(head.right); } else { res += "#!"; } } return res; } //Inverse sequence public static Node reconByLevelString(String levelString) { String[] value = levelString.split("!"); int index = 0; Node head = generateNodeByString(value[index++]); Queue<Node> queue = new LinkedList<Node>(); if (head != null) { queue.offer(head); //Head in team } Node node = null; while (!queue.isEmpty()) { //Order: left, right, left child, right child node = queue.poll(); //take node.left = generateNodeByString(value[index++]); node.right = generateNodeByString(value[index++]); if (node.left != null) { //Let the kids go? queue.offer(node.left); } if (node.right != null) { queue.offer(node.right); } } return head; } private static Node generateNodeByString(String string) { //Judge whether it is empty if (string.equals("#")) { return null; } return new Node(Integer.valueOf(string)); }
test
Node node1 = new Node(1); Node node2 = new Node(2); Node node3 = new Node(3); Node node4 = new Node(4); Node node5 = new Node(5); Node head1 = new Node(0); Node head2 = new Node(0); Node head3 = new Node(0); head1.left = node1; head1.right = node2; node1.left = node3; node1.right = node4; node2.left = node5; head2.left = node1; head2.left.left = node3; System.out.println(serialByPre(head1)); System.out.println("= = = = = = ="); System.out.println(serialByPre(head2)); System.out.println("= = = = = = ="); System.out.println(reconByPreString(serialByPre(head1)).value); System.out.println("= = = = = = ="); System.out.println(serialBylevel(head1)); System.out.println("= = = = = = ="); System.out.println(serialBylevel(head2)); System.out.println("= = = = = = ="); System.out.println(reconByLevelString(serialBylevel(head1)).value);