-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSerializeAndReconstructTree.java
More file actions
68 lines (56 loc) · 1.66 KB
/
Copy pathSerializeAndReconstructTree.java
File metadata and controls
68 lines (56 loc) · 1.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
package tree;
import org.junit.Test;
import utils.TreeNodeUtil;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.Queue;
/**
* @Author: wei1
* @Date: Create in 2018/11/23 16:33
* @Description:
*/
public class SerializeAndReconstructTree {
//记住按什么序列化,就按什么反序列化就可以了
public String serialByPre(TreeNode head) {
if (head == null) {
return "#!";
}
String s = head.val + "!";
String s1 = serialByPre(head.left);
System.out.print(s+" ");
String s2 = serialByPre(head.right);
return s + s1 + s2;
}
//这个感觉真是一个好方法
public TreeNode reconPreOrder(Queue<String> queue) {
TreeNode node = null;
if (!queue.isEmpty()) {
String s = queue.poll();
if (!s.equals("#")) {
node = new TreeNode(Integer.parseInt(s));
}
}
if (node == null) {
return null;
}
node.left = reconPreOrder(queue);
node.right = reconPreOrder(queue);
return node;
}
@Test
public void test() {
TreeNode head = TreeNodeUtil.getTree();
PrintBinaryTree.printTree(head);
System.out.println();
String s = serialByPre(head);
System.out.println();
System.out.println(s);
String[] strings = s.split("!");
Queue<String> queue = new LinkedList<String>(Arrays.asList(strings));
TreeNode newHead = reconPreOrder(queue);
System.out.println();
serialByPre(newHead);
System.out.println();
PrintBinaryTree.printTree(newHead);
}
}