Skip to content

Commit d4d498f

Browse files
committed
feat: add solutions to lc problem: No.0449
No.0449.Serialize and Deserialize BST
1 parent cf67e29 commit d4d498f

File tree

4 files changed

+335
-45
lines changed

4 files changed

+335
-45
lines changed

solution/0400-0499/0449.Serialize and Deserialize BST/README.md

+119-2
Original file line numberDiff line numberDiff line change
@@ -53,15 +53,132 @@
5353
<!-- 这里可写当前语言的特殊实现逻辑 -->
5454

5555
```python
56-
56+
# Definition for a binary tree node.
57+
# class TreeNode:
58+
# def __init__(self, x):
59+
# self.val = x
60+
# self.left = None
61+
# self.right = None
62+
63+
class Codec:
64+
65+
def serialize(self, root: TreeNode) -> str:
66+
"""Encodes a tree to a single string.
67+
"""
68+
def dfs(root):
69+
if root is None:
70+
return
71+
nonlocal t
72+
t.append(str(root.val))
73+
t.append(',')
74+
dfs(root.left)
75+
dfs(root.right)
76+
77+
if root is None:
78+
return ''
79+
t = []
80+
dfs(root)
81+
return ''.join(t[:-1])
82+
83+
def deserialize(self, data: str) -> TreeNode:
84+
"""Decodes your encoded data to tree.
85+
"""
86+
def build(s, l, r):
87+
if l > r:
88+
return None
89+
root = TreeNode(int(s[l]))
90+
idx = r + 1
91+
for i in range(l + 1, r + 1):
92+
if int(s[i]) > root.val:
93+
idx = i
94+
break
95+
root.left = build(s, l + 1, idx - 1)
96+
root.right = build(s, idx, r)
97+
return root
98+
99+
if not data:
100+
return None
101+
s = data.split(',')
102+
return build(s, 0, len(s) - 1)
103+
104+
105+
# Your Codec object will be instantiated and called as such:
106+
# Your Codec object will be instantiated and called as such:
107+
# ser = Codec()
108+
# deser = Codec()
109+
# tree = ser.serialize(root)
110+
# ans = deser.deserialize(tree)
111+
# return ans
57112
```
58113

59114
### **Java**
60115

61116
<!-- 这里可写当前语言的特殊实现逻辑 -->
62117

63118
```java
64-
119+
/**
120+
* Definition for a binary tree node.
121+
* public class TreeNode {
122+
* int val;
123+
* TreeNode left;
124+
* TreeNode right;
125+
* TreeNode(int x) { val = x; }
126+
* }
127+
*/
128+
public class Codec {
129+
130+
// Encodes a tree to a single string.
131+
public String serialize(TreeNode root) {
132+
if (root == null) {
133+
return "";
134+
}
135+
StringBuilder sb = new StringBuilder();
136+
dfs(root, sb);
137+
return sb.substring(0, sb.length() - 1);
138+
}
139+
140+
private void dfs(TreeNode root, StringBuilder sb) {
141+
if (root == null) {
142+
return;
143+
}
144+
sb.append(root.val).append(",");
145+
dfs(root.left, sb);
146+
dfs(root.right, sb);
147+
}
148+
149+
// Decodes your encoded data to tree.
150+
public TreeNode deserialize(String data) {
151+
if (data == null || "".equals(data)) {
152+
return null;
153+
}
154+
String[] s = data.split(",");
155+
return build(s, 0, s.length - 1);
156+
}
157+
158+
private TreeNode build(String[] s, int l, int r) {
159+
if (l > r) {
160+
return null;
161+
}
162+
int idx = r + 1;
163+
TreeNode root = new TreeNode(Integer.valueOf(s[l]));
164+
for (int i = l + 1; i <= r; ++i) {
165+
if (Integer.valueOf(s[i]) > root.val) {
166+
idx = i;
167+
break;
168+
}
169+
}
170+
root.left = build(s, l + 1, idx - 1);
171+
root.right = build(s, idx, r);
172+
return root;
173+
}
174+
}
175+
176+
// Your Codec object will be instantiated and called as such:
177+
// Codec ser = new Codec();
178+
// Codec deser = new Codec();
179+
// String tree = ser.serialize(root);
180+
// TreeNode ans = deser.deserialize(tree);
181+
// return ans;
65182
```
66183

67184
### **...**

solution/0400-0499/0449.Serialize and Deserialize BST/README_EN.md

+119-2
Original file line numberDiff line numberDiff line change
@@ -34,13 +34,130 @@
3434
### **Python3**
3535

3636
```python
37-
37+
# Definition for a binary tree node.
38+
# class TreeNode:
39+
# def __init__(self, x):
40+
# self.val = x
41+
# self.left = None
42+
# self.right = None
43+
44+
class Codec:
45+
46+
def serialize(self, root: TreeNode) -> str:
47+
"""Encodes a tree to a single string.
48+
"""
49+
def dfs(root):
50+
if root is None:
51+
return
52+
nonlocal t
53+
t.append(str(root.val))
54+
t.append(',')
55+
dfs(root.left)
56+
dfs(root.right)
57+
58+
if root is None:
59+
return ''
60+
t = []
61+
dfs(root)
62+
return ''.join(t[:-1])
63+
64+
def deserialize(self, data: str) -> TreeNode:
65+
"""Decodes your encoded data to tree.
66+
"""
67+
def build(s, l, r):
68+
if l > r:
69+
return None
70+
root = TreeNode(int(s[l]))
71+
idx = r + 1
72+
for i in range(l + 1, r + 1):
73+
if int(s[i]) > root.val:
74+
idx = i
75+
break
76+
root.left = build(s, l + 1, idx - 1)
77+
root.right = build(s, idx, r)
78+
return root
79+
80+
if not data:
81+
return None
82+
s = data.split(',')
83+
return build(s, 0, len(s) - 1)
84+
85+
86+
# Your Codec object will be instantiated and called as such:
87+
# Your Codec object will be instantiated and called as such:
88+
# ser = Codec()
89+
# deser = Codec()
90+
# tree = ser.serialize(root)
91+
# ans = deser.deserialize(tree)
92+
# return ans
3893
```
3994

4095
### **Java**
4196

4297
```java
43-
98+
/**
99+
* Definition for a binary tree node.
100+
* public class TreeNode {
101+
* int val;
102+
* TreeNode left;
103+
* TreeNode right;
104+
* TreeNode(int x) { val = x; }
105+
* }
106+
*/
107+
public class Codec {
108+
109+
// Encodes a tree to a single string.
110+
public String serialize(TreeNode root) {
111+
if (root == null) {
112+
return "";
113+
}
114+
StringBuilder sb = new StringBuilder();
115+
dfs(root, sb);
116+
return sb.substring(0, sb.length() - 1);
117+
}
118+
119+
private void dfs(TreeNode root, StringBuilder sb) {
120+
if (root == null) {
121+
return;
122+
}
123+
sb.append(root.val).append(",");
124+
dfs(root.left, sb);
125+
dfs(root.right, sb);
126+
}
127+
128+
// Decodes your encoded data to tree.
129+
public TreeNode deserialize(String data) {
130+
if (data == null || "".equals(data)) {
131+
return null;
132+
}
133+
String[] s = data.split(",");
134+
return build(s, 0, s.length - 1);
135+
}
136+
137+
private TreeNode build(String[] s, int l, int r) {
138+
if (l > r) {
139+
return null;
140+
}
141+
int idx = r + 1;
142+
TreeNode root = new TreeNode(Integer.valueOf(s[l]));
143+
for (int i = l + 1; i <= r; ++i) {
144+
if (Integer.valueOf(s[i]) > root.val) {
145+
idx = i;
146+
break;
147+
}
148+
}
149+
root.left = build(s, l + 1, idx - 1);
150+
root.right = build(s, idx, r);
151+
return root;
152+
}
153+
}
154+
155+
// Your Codec object will be instantiated and called as such:
156+
// Codec ser = new Codec();
157+
// Codec deser = new Codec();
158+
// String tree = ser.serialize(root);
159+
// TreeNode ans = deser.deserialize(tree);
160+
// return ans;
44161
```
45162

46163
### **...**

solution/0400-0499/0449.Serialize and Deserialize BST/Solution.java

+41-41
Original file line numberDiff line numberDiff line change
@@ -9,55 +9,55 @@
99
*/
1010
public class Codec {
1111

12-
// Encodes a tree to a single string.
13-
public String serialize(TreeNode root) {
14-
if (root == null) {
15-
return "";
16-
}
17-
StringBuilder sb = new StringBuilder();
18-
robot(root, sb);
19-
return sb.substring(0, sb.length() - 1);
12+
// Encodes a tree to a single string.
13+
public String serialize(TreeNode root) {
14+
if (root == null) {
15+
return "";
2016
}
17+
StringBuilder sb = new StringBuilder();
18+
dfs(root, sb);
19+
return sb.substring(0, sb.length() - 1);
20+
}
2121

22-
private void robot(TreeNode root, StringBuilder sb) {
23-
if (root == null) {
24-
return;
25-
}
26-
sb.append(root.val).append(",");
27-
robot(root.left, sb);
28-
robot(root.right, sb);
22+
private void dfs(TreeNode root, StringBuilder sb) {
23+
if (root == null) {
24+
return;
2925
}
26+
sb.append(root.val).append(",");
27+
dfs(root.left, sb);
28+
dfs(root.right, sb);
29+
}
3030

31-
// Decodes your encoded data to tree.
32-
public TreeNode deserialize(String data) {
33-
if (data == null || Objects.equals(data, "")) {
34-
return null;
35-
}
36-
String[] pre = data.split(",");
37-
return build(pre, 0, pre.length - 1);
31+
// Decodes your encoded data to tree.
32+
public TreeNode deserialize(String data) {
33+
if (data == null || "".equals(data)) {
34+
return null;
3835
}
36+
String[] s = data.split(",");
37+
return build(s, 0, s.length - 1);
38+
}
3939

40-
private TreeNode build(String[] pre, int start, int end) {
41-
if (start > end) {
42-
return null;
43-
}
44-
TreeNode root = new TreeNode(Integer.valueOf(pre[start]));
45-
46-
int index = end + 1;
47-
for (int i = start + 1; i <= end; i++) {
48-
if (Integer.valueOf(pre[i]) > root.val) {
49-
index = i;
50-
break;
51-
}
40+
private TreeNode build(String[] s, int l, int r) {
41+
if (l > r) {
42+
return null;
43+
}
44+
int idx = r + 1;
45+
TreeNode root = new TreeNode(Integer.valueOf(s[l]));
46+
for (int i = l + 1; i <= r; ++i) {
47+
if (Integer.valueOf(s[i]) > root.val) {
48+
idx = i;
49+
break;
5250
}
53-
54-
root.left = build(pre, start + 1, index - 1);
55-
root.right = build(pre, index, end);
56-
return root;
5751
}
58-
52+
root.left = build(s, l + 1, idx - 1);
53+
root.right = build(s, idx, r);
54+
return root;
5955
}
56+
}
6057

6158
// Your Codec object will be instantiated and called as such:
62-
// Codec codec = new Codec();
63-
// codec.deserialize(codec.serialize(root));
59+
// Codec ser = new Codec();
60+
// Codec deser = new Codec();
61+
// String tree = ser.serialize(root);
62+
// TreeNode ans = deser.deserialize(tree);
63+
// return ans;

0 commit comments

Comments
 (0)