-
Notifications
You must be signed in to change notification settings - Fork 266
Expand file tree
/
Copy path37.java
More file actions
37 lines (35 loc) · 1.13 KB
/
37.java
File metadata and controls
37 lines (35 loc) · 1.13 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
public class Codec {
// Encodes a tree to a single string.
//递归; 二叉树的遍历, 采用前序遍历, 1)要保留结构信息(除了左右孩子, 还要考虑null), 2)要设置间隔符, 从而区分不同的val
public String serialize(TreeNode root) {
//base case
if(root==null)
return "#!";
StringBuilder sb = new StringBuilder();
sb.append(root.val).append("!");
sb.append(serialize(root.left));
sb.append(serialize(root.right));
return sb.toString();
}
// Decodes your encoded data to tree.
public TreeNode deserialize(String data) {
String[] strs = data.split("!");
return core(strs);
}
private int i;
//二叉树的遍历, 前序遍历, 递归
private TreeNode core(String[] strs){
//base case
if(i==strs.length)
return null;
if(strs[i].equals("#")){
i++; //索引++
return null;
}
TreeNode root = new TreeNode(Integer.parseInt(strs[i]));
i++;
root.left = core(strs);
root.right = core(strs);
return root;
}
}