forked from jsjtzyy/LeetCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLC449_SerializeDeserializeBST.java
More file actions
50 lines (48 loc) · 1.59 KB
/
LC449_SerializeDeserializeBST.java
File metadata and controls
50 lines (48 loc) · 1.59 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
import java.util.*;
public class LC449_SerializeDeserializeBST {
// Encodes a tree to a single string.
public String serialize(TreeNode root) {
if(root == null) return null;
String res = String.valueOf(root.val);
String left = serialize(root.left);
String right = serialize(root.right);
if(left != null) res = "(" + left + ")" + res;
if(right != null) res = res + "(" + right + ")";
return res;
}
// Decodes your encoded data to tree.
public TreeNode deserialize(String data) {
TreeNode res = null;
if(data == null) return res;
int len = data.length();
int val = 0, index = 0;
int leftCnt = 0;
char ch = ' ';
if(data.charAt(0) != '('){
//ch = data.charAt(index);
while(index < len && data.charAt(index) != '('){
val = val * 10 + (data.charAt(index) - '0');
++index;
}
res = new TreeNode(val);
}else{
leftCnt = 1;
index = 1;
while(leftCnt != 0){
ch = data.charAt(index);
if(ch == '(') leftCnt++;
if(ch == ')') leftCnt--;
++index;
}
int leftPtr = index;
while(index < len && data.charAt(index) != '('){
val = val * 10 + (data.charAt(index) - '0');
++index;
}
res = new TreeNode(val);
res.left = deserialize(data.substring(1, leftPtr - 1)); // remove the outermost "(" and ")"
}
if(index != len) res.right = deserialize(data.substring(index + 1, len - 1));
return res;
}
}