/**
* 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}
*/
const serialize = function(root) {
const str = [];
tostr(root, str);
return str;
};
const tostr = function(root, str) {
if (root === null) {
str.push('X');
} else {
str.push(root.val);
tostr(root.left, str);
tostr(root.right, str);
}
}
/**
* Decodes your encoded data to tree.
*
* @param {string} data
* @return {TreeNode}
*/
const deserialize = function(data) {
var val = data.shift();
if (val === "X") {
return null;
} else {
const node = new TreeNode(val);
node.left = deserialize(data);
node.right = deserialize(data);
return node;
}
};
/**
* Your functions will be called as such:
* deserialize(serialize(root));
*/