forked from xcatliu/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
83 additions
and
0 deletions.
There are no files selected for viewing
81 changes: 81 additions & 0 deletions
81
problems/297-serialize-and-deserialize-binary-tree/index.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
/** | ||
* https://leetcode.com/problems/serialize-and-deserialize-binary-tree/ | ||
* | ||
* 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. | ||
* | ||
* Design an algorithm to serialize and deserialize a binary tree. | ||
* There is no restriction on how your serialization/deserialization algorithm | ||
* should work. | ||
* 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. | ||
* | ||
* For example, you may serialize the following tree | ||
* | ||
* 1 | ||
* / \ | ||
* 2 3 | ||
* / \ | ||
* 4 5 | ||
* as "[1,2,3,null,null,4,5]", | ||
* just the same as how LeetCode OJ serializes a binary tree. | ||
* You do not necessarily need to follow this format, | ||
* so please be creative and come up with different approaches yourself. | ||
* | ||
* Note: Do not use class member/global/static variables to store states. | ||
* Your serialize and deserialize algorithms should be stateless. | ||
*/ | ||
|
||
/** | ||
* You should comment the following function to pass the leetcode tests. | ||
* Definition for a binary tree node. | ||
*/ | ||
var TreeNode = require('../../structures/TreeNode'); | ||
|
||
/** | ||
* Definition for a binary tree node. | ||
* function TreeNode(val) { | ||
* this.val = val; | ||
* this.left = this.right = null; | ||
* } | ||
*/ | ||
|
||
/** | ||
* Encodes a tree to a single string. | ||
* | ||
* @param {TreeNode} root | ||
* @return {string} | ||
*/ | ||
var serialize = exports.serialize = function (root) { | ||
return JSON.stringify(ser(root)); | ||
|
||
function ser(r) { | ||
if (r === null) return null; | ||
return [r.val, ser(r.left), ser(r.right)]; | ||
} | ||
}; | ||
|
||
/** | ||
* Decodes your encoded data to tree. | ||
* | ||
* @param {string} data | ||
* @return {TreeNode} | ||
*/ | ||
var deserialize = exports.deserialize = function (data) { | ||
return deser(JSON.parse(data)); | ||
|
||
function deser(d) { | ||
if (d === null) return null; | ||
var root = new TreeNode(d[0]); | ||
root.left = deser(d[1]); | ||
root.right = deser(d[2]); | ||
return root; | ||
} | ||
}; | ||
|
||
/** | ||
* Your functions will be called as such: | ||
* deserialize(serialize(root)); | ||
*/ |
2 changes: 2 additions & 0 deletions
2
problems/297-serialize-and-deserialize-binary-tree/testcases.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
module.exports = [ | ||
]; |