Working with Graphs & Trees
What tree traversal order is used in this serialization format?
function serialize(root) {
if (!root) return 'null';
return `${root.value},${serialize(root.left)},${serialize(root.right)}`;
}
function deserialize(data) {
const values = data.split(',');
let index = 0;
function dfs() {
if (values[index] === 'null') {
index++;
return null;
}
const node = { value: parseInt(values[index++]), left: null, right: null };
node.left = dfs();
node.right = dfs();
return node;
}
return dfs();
}