Skip to content

Commit bcf468c

Browse files
committed
Add solution #297
1 parent d55705b commit bcf468c

File tree

2 files changed

+43
-0
lines changed

2 files changed

+43
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,7 @@
229229
290|[Word Pattern](./0290-word-pattern.js)|Easy|
230230
292|[Nim Game](./0292-nim-game.js)|Easy|
231231
295|[Find Median from Data Stream](./0295-find-median-from-data-stream.js)|Hard|
232+
297|[Serialize and Deserialize Binary Tree](./0297-serialize-and-deserialize-binary-tree.js)|Hard|
232233
303|[Range Sum Query - Immutable](./0303-range-sum-query-immutable.js)|Easy|
233234
306|[Additive Number](./0306-additive-number.js)|Medium|
234235
316|[Remove Duplicate Letters](./0316-remove-duplicate-letters.js)|Medium|
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/**
2+
* 297. Serialize and Deserialize Binary Tree
3+
* https://leetcode.com/problems/serialize-and-deserialize-binary-tree/
4+
* Difficulty: Hard
5+
*
6+
* Serialization is the process of converting a data structure or object into a sequence of bits so
7+
* that it can be stored in a file or memory buffer, or transmitted across a network connection link
8+
* to be reconstructed later in the same or another computer environment.
9+
*
10+
* Design an algorithm to serialize and deserialize a binary tree. There is no restriction on how
11+
* your serialization/deserialization algorithm should work. You just need to ensure that a binary
12+
* tree can be serialized to a string and this string can be deserialized to the original tree
13+
* structure.
14+
*/
15+
16+
/**
17+
* Definition for a binary tree node.
18+
* function TreeNode(val) {
19+
* this.val = val;
20+
* this.left = this.right = null;
21+
* }
22+
*/
23+
24+
/**
25+
* Encodes a tree to a single string.
26+
*
27+
* @param {TreeNode} root
28+
* @return {string}
29+
*/
30+
var serialize = function(root) {
31+
return JSON.stringify(root);
32+
};
33+
34+
/**
35+
* Decodes your encoded data to tree.
36+
*
37+
* @param {string} data
38+
* @return {TreeNode}
39+
*/
40+
var deserialize = function(data) {
41+
return JSON.parse(data);
42+
};

0 commit comments

Comments
 (0)