forked from DreamCats/java-notes
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathT37.java
More file actions
33 lines (27 loc) · 698 Bytes
/
Copy pathT37.java
File metadata and controls
33 lines (27 loc) · 698 Bytes
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
package books;
/**
* @program JavaBooks
* @description: 序列化二叉树
* @author: mf
* @create: 2019/09/18 10:02
*/
/*
请实现两个函数,分别用来序列化和反序列化二叉树。
*/
public class T37 {
public static void main(String[] args) {
int[] pre = {1, 2, 4, 3, 5, 6};
int[] in = {4, 2, 1, 5, 3, 6};
TreeNode root = TreeNode.setBinaryTree(pre, in);
serialize(root);
}
private static void serialize(TreeNode root) {
if (root == null) {
System.out.print("$,");
return;
}
System.out.print(root.val + ",");
serialize(root.left);
serialize(root.right);
}
}