-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path61.java
More file actions
42 lines (39 loc) · 1008 Bytes
/
61.java
File metadata and controls
42 lines (39 loc) · 1008 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
34
35
36
37
38
39
40
41
42
/*
public class TreeNode {
int val = 0;
TreeNode left = null;
TreeNode right = null;
public TreeNode(int val) {
this.val = val;
}
}
*/
public class Solution {
int index=-1;
String Serialize(TreeNode root) {
//将给定的树前序遍历转换成字符,用,隔开
StringBuffer st=new StringBuffer();
if(root==null)
{
st.append("#,");
return st.toString();
}
st.append(root.val+",");
st.append(Serialize(root.left));
st.append(Serialize(root.right));
return st.toString();
}
TreeNode Deserialize(String str) {
//将给定的字符串反序列化为树
index++;
String []tmp=str.split(",");
TreeNode root=null;
if(!tmp[index].equals("#"))
{
root=new TreeNode(Integer.valueOf(tmp[index]));
root.left=Deserialize(str);
root.right=Deserialize(str);
}
return root;
}
}