Skip to content

Commit 48ec2b5

Browse files
committed
Leetcode 297. Serialize and Deserialize a binary tree
1 parent cb6d60b commit 48ec2b5

File tree

11 files changed

+231
-0
lines changed

11 files changed

+231
-0
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -121,5 +121,6 @@ ENV/
121121
*.out
122122
*.app
123123
*.h.gch
124+
*.class
124125

125126
.DS_Store

.idea/.gitignore

+3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/DataStructures_Algorithms.iml

+9
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/misc.xml

+7
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/modules.xml

+8
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/vcs.xml

+6
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.vscode/c_cpp_properties.json

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
{
2+
"configurations": [
3+
{
4+
"name": "Mac",
5+
"includePath": [
6+
"${workspaceFolder}/**",
7+
"${workspaceFolder}/"
8+
],
9+
"defines": [],
10+
"macFrameworkPath": [
11+
"/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.14.sdk/System/Library/Frameworks"
12+
],
13+
"compilerPath": "/usr/bin/clang",
14+
"cStandard": "c11",
15+
"cppStandard": "c++17",
16+
"intelliSenseMode": "clang-x64"
17+
}
18+
],
19+
"version": 4
20+
}

.vscode/launch.json

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{
2+
"configurations": [
3+
{
4+
"type": "java",
5+
"name": "CodeLens (Launch) - main",
6+
"request": "launch",
7+
"mainClass": "main",
8+
"projectName": "DataStructures_Algorithms_89cff2ad"
9+
},
10+
{
11+
"type": "java",
12+
"name": "CodeLens (Launch) - LinkedList",
13+
"request": "launch",
14+
"mainClass": "LinkedList"
15+
}
16+
]
17+
}

.vscode/settings.json

+78
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
{
2+
"files.associations": {
3+
"__bit_reference": "cpp",
4+
"__config": "cpp",
5+
"__debug": "cpp",
6+
"__errc": "cpp",
7+
"__functional_base": "cpp",
8+
"__hash_table": "cpp",
9+
"__locale": "cpp",
10+
"__mutex_base": "cpp",
11+
"__node_handle": "cpp",
12+
"__nullptr": "cpp",
13+
"__split_buffer": "cpp",
14+
"__string": "cpp",
15+
"__threading_support": "cpp",
16+
"__tree": "cpp",
17+
"__tuple": "cpp",
18+
"algorithm": "cpp",
19+
"array": "cpp",
20+
"atomic": "cpp",
21+
"bitset": "cpp",
22+
"cctype": "cpp",
23+
"chrono": "cpp",
24+
"cinttypes": "cpp",
25+
"cmath": "cpp",
26+
"complex": "cpp",
27+
"cstdarg": "cpp",
28+
"cstddef": "cpp",
29+
"cstdint": "cpp",
30+
"cstdio": "cpp",
31+
"cstdlib": "cpp",
32+
"cstring": "cpp",
33+
"ctime": "cpp",
34+
"cwchar": "cpp",
35+
"cwctype": "cpp",
36+
"deque": "cpp",
37+
"exception": "cpp",
38+
"optional": "cpp",
39+
"fstream": "cpp",
40+
"functional": "cpp",
41+
"future": "cpp",
42+
"initializer_list": "cpp",
43+
"iomanip": "cpp",
44+
"ios": "cpp",
45+
"iosfwd": "cpp",
46+
"iostream": "cpp",
47+
"istream": "cpp",
48+
"iterator": "cpp",
49+
"limits": "cpp",
50+
"list": "cpp",
51+
"locale": "cpp",
52+
"map": "cpp",
53+
"memory": "cpp",
54+
"mutex": "cpp",
55+
"new": "cpp",
56+
"numeric": "cpp",
57+
"ostream": "cpp",
58+
"queue": "cpp",
59+
"ratio": "cpp",
60+
"regex": "cpp",
61+
"set": "cpp",
62+
"sstream": "cpp",
63+
"stdexcept": "cpp",
64+
"streambuf": "cpp",
65+
"string": "cpp",
66+
"string_view": "cpp",
67+
"system_error": "cpp",
68+
"thread": "cpp",
69+
"tuple": "cpp",
70+
"type_traits": "cpp",
71+
"typeindex": "cpp",
72+
"typeinfo": "cpp",
73+
"unordered_map": "cpp",
74+
"unordered_set": "cpp",
75+
"utility": "cpp",
76+
"vector": "cpp"
77+
}
78+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
/**
2+
* Serialization is the process of converting a data structure or object into a sequence of bits so that it can be stored in a file or memory buffer, or transmitted across a network connection link to be reconstructed later in the same or another computer environment.
3+
*
4+
* Design an algorithm to serialize and deserialize a binary tree.
5+
* There is no restriction on how your serialization/deserialization algorithm should work.
6+
* You just need to ensure that a binary tree can be serialized to a string and this string can be deserialized to the original tree structure.
7+
*
8+
* Example:
9+
*
10+
* You may serialize the following tree:
11+
*
12+
* 1
13+
* / \
14+
* 2 3
15+
* / \
16+
* 4 5
17+
*
18+
* as "[1,2,3,null,null,4,5]"
19+
*
20+
* Clarification: The above format is the same as how LeetCode serializes a binary tree.
21+
* You do not necessarily need to follow this format, so please be creative and come up with different approaches yourself.
22+
*
23+
* Note: Do not use class member/global/static variables to store states. Your serialize and deserialize algorithms should be stateless.
24+
*/
25+
26+
/**
27+
* Definition for a binary tree node.
28+
* public class TreeNode {
29+
* int val;
30+
* TreeNode left;
31+
* TreeNode right;
32+
* TreeNode(int x) { val = x; }
33+
* }
34+
*/
35+
public class Codec {
36+
37+
private static final String NULL = "X";
38+
private static final String DELIMIT = ",";
39+
40+
// Encodes a tree to a single string.
41+
public String serialize(TreeNode root) {
42+
if (root == null)
43+
return NULL + DELIMIT;
44+
45+
String leftSerialize = serialize(root.left);
46+
String rightSerialize = serialize(root.right);
47+
48+
return root.val + DELIMIT + leftSerialize + rightSerialize;
49+
}
50+
51+
// Decodes your encoded data to tree.
52+
public TreeNode deserialize(String data) {
53+
Queue<String> nodesToSerialize = new LinkedList<>();
54+
55+
nodesToSerialize.addAll(Arrays.asList(data.split(DELIMIT))); //split with the comma's
56+
57+
return deserializeHelper(nodesToSerialize);
58+
}
59+
60+
private TreeNode deserializeHelper(Queue<String> nodesToSerialize)
61+
{
62+
String valueForNode = nodesToSerialize.poll();
63+
64+
if (valueForNode.equals(NULL)) {
65+
return null;
66+
}
67+
68+
TreeNode newNode = new TreeNode(Integer.valueOf(valueForNode));
69+
70+
newNode.left = deserializeHelper(nodesToSerialize);
71+
newNode.right = deserializeHelper(nodesToSerialize);
72+
73+
return newNode;
74+
75+
}
76+
}
77+
78+
// Your Codec object will be instantiated and called as such:
79+
// Codec codec = new Codec();
80+
// codec.deserialize(codec.serialize(root));

LeetCode/README.md

+2
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,8 @@
5454
- [x] [Lowest Common Ancestor of a Binary Tree](https://github.com/kalpak92/DataStructures_Algorithms/tree/master/LeetCode/236.%20Lowest%20Common%20Ancestor%20of%20a%20Binary%20Tree)
5555
- [x] [Binary Tree Zigzag Level order traversal](https://github.com/kalpak92/DataStructures_Algorithms/tree/master/LeetCode/103.%20Binary%20Tree%20Zigzag%20Level%20Order%20Traversal)
5656
- [x] [Subtree of another tree](https://github.com/kalpak92/DataStructures_Algorithms/tree/master/LeetCode/572.%20Subtree%20of%20Another%20Tree)
57+
- [x] [Serialize and Deserialize Binary Tree]()
58+
5759
## Strings
5860
- [x] [Longest Palindrome](https://github.com/kalpak92/DataStructures_Algorithms/tree/master/LeetCode/409.%20Longest%20Palindrome)
5961
- [x] [Palindrome Permutation](https://github.com/kalpak92/DataStructures_Algorithms/tree/master/LeetCode/266.%20Palindrome%20Permutation)

0 commit comments

Comments
 (0)