-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsolution37.java
More file actions
37 lines (33 loc) · 970 Bytes
/
Copy pathsolution37.java
File metadata and controls
37 lines (33 loc) · 970 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
package nowcoder;
public class solution37 {
private String deserializeStr;
public String Serialize(TreeNode root) {//前序遍历
if(root == null)
{
return "#";
}
return root.val+" "+Serialize(root.left)+" "+Serialize(root.right);
}
TreeNode Deserialize(String str) {
deserializeStr = str;
return Deserialize();
}
private TreeNode Deserialize() {
if(deserializeStr.length()==0)
{
return null;
}
int index = deserializeStr.indexOf(" ") ;
String node = index==-1?deserializeStr:deserializeStr.substring(0,index);
deserializeStr = index==-1?"":deserializeStr.substring(index+1);
if(node.equals("#"))
{
return null;
}
int val = Integer.valueOf(node);
TreeNode t = new TreeNode(val);
t.left = Deserialize();
t.right = Deserialize();
return t;
}
}