Last active
May 1, 2020 21:14
-
-
Save Gosilama/ff81227d55665dc3aaaa9c7f19d4810a to your computer and use it in GitHub Desktop.
Given the root to a binary tree, implement serialize(root), which serializes the tree into a string, and deserialize(s), which deserializes the string back into the tree. For example, given the following Node class class Node: def __init__(self, val, left=None, right=None): self.val = val self.left = left self.right = right The following test sh…
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
/** | |
Given the root to a binary tree, implement serialize(root), which serializes the tree into a string, and deserialize(s), which deserializes the string back into the tree. | |
For example, given the following Node class | |
class Node: | |
def __init__(self, val, left=None, right=None): | |
self.val = val | |
self.left = left | |
self.right = right | |
The following test should pass: | |
node = Node('root', Node('left', Node('left.left')), Node('right')) | |
assert deserialize(serialize(node)).left.left.val == 'left.left' | |
*/ | |
const serialize = (root) => { | |
const vals = []; | |
const encode = node => { | |
if (node) { | |
vals.push(node.val.toString()); | |
encode(node.left); | |
encode(node.right); | |
} else { | |
vals.push('-') | |
} | |
} | |
encode(root); | |
return vals.toString(); | |
} | |
const deserialize = (serializedBt) => { | |
const vals = serializedBt.split(','); | |
const decode = () => { | |
const val = vals.shift(); | |
if (val === '-') return; | |
const node = { | |
val, | |
}; | |
node.left = decode(); | |
node.right = decode(); | |
return node; | |
} | |
return decode(); | |
} | |
// Test | |
class Node { | |
constructor(val, left=null, right=null) { | |
this.val = val; | |
this.left = left; | |
this.right = right; | |
} | |
} | |
const leftNode = new Node('left', new Node('left.left')); | |
const rightNode = new Node('right'); | |
const node = new Node('root', leftNode, rightNode); | |
const serializedBt = serialize(node); | |
deserialize(serializedBt); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment