-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathL449.java
More file actions
59 lines (55 loc) · 2.06 KB
/
L449.java
File metadata and controls
59 lines (55 loc) · 2.06 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
51
52
53
54
55
56
57
58
59
import java.util.Arrays;
import java.util.List;
import java.util.Stack;
import java.util.stream.Collectors;
public class L449 {
/**
* 449. Serialize and Deserialize BST https://leetcode.com/problems/serialize-and-deserialize-bst/
*
* @timeComplexity O(n) serialize O(n) deserialize
* @spaceComplexity O(n) for output On) for deserialize
*/
static class Codec {
// Encodes a tree to a single string.
public String serialize(TreeNode root) {
StringBuilder sb = new StringBuilder();
traversePreorder(root, sb);
return sb.toString();
}
public void traversePreorder(TreeNode root, StringBuilder sb) {
if (root != null) {
sb.append(String.valueOf(root.val));
sb.append(" ");
traversePreorder(root.left, sb);
traversePreorder(root.right, sb);
}
}
// Decodes your encoded data to tree.
public TreeNode deserialize(String data) {
// Empty string gives a null tree
if (data.length() == 0) {
return null;
}
List<Integer> preorder = Arrays.stream(data.split(" ")).map(x -> Integer.valueOf(x)).collect(Collectors.toList());
Stack<TreeNode> s = new Stack<>();
TreeNode root = new TreeNode(preorder.get(0));
s.push(root);
for (int i = 1; i < preorder.size(); i++) {
TreeNode temp = null;
while (!s.isEmpty() && preorder.get(i) > s.peek().val)
temp = s.pop();
if (temp != null) {
temp.right = new TreeNode(preorder.get(i));
s.push(temp.right);
} else {
s.peek().left = new TreeNode(preorder.get(i));
s.push(s.peek().left);
}
}
return root;
}
}
// Your Codec object will be instantiated and called as such:
// Codec codec = new Codec();
// codec.deserialize(codec.serialize(root));
}