serialize and deserialize binary tree
2022年5月1日小于 1 分钟
serialize and deserialize binary tree
Question
Can you transform(serialize) a binary tree into a string and restore(deserialize) a binary tree from the string? Just like what JSON.stringify() and JSON.parse() do.
For example, for a tree from 91. invert a binary tree
BFE.dev would serialize it to [1,2,3,4,null,null,5,6,7,8,null,null,null,null,9]
But there are more ways of doing it rather than above, any would be fine as long as your deserialize()
and serialize()
work as a pair.
Your code is tested like this:
const tree1 = ...
expect(typeof serialize(tree1)).toBe('string')
const tree2 = deserialize(serialize(tree1))
expect(isIdentical(tree1, tree2)).toBe(true)
Binary tree in this problem consists of value of integers.
Code
// This is the class for the node
// you can use this directly as it is bundled with your code
// class Node {
// value: number
// left: null | Node
// right: null | Node
// constructor(val) {
// this.value = val
// this.left = null
// this.right = null
// }
// }
/**
* @param {Node} root
* @return {string}
*/
function serialize(root) {
// your code here
}
/**
* @param {string} str
* @return {Node}
*/
function deserialize(str) {
// your code here
}