function TreeNode(x) {
this.val = x;
this.left = null;
this.right = null;
}
function Serialize(pRoot)
{
// write code here
if(!pRoot){
return '';
}
let str = '';
function dfs(pRoot){
if(!pRoot){
str += ',#';
return;
}
str +=','+pRoot.val;
dfs(pRoot.left);
dfs(pRoot.right);
}
dfs(pRoot);
console.log(str);
return str.slice(1);
}
function Deserialize(s)
{
// write code here
if(s === ''){
return null;
}
let str = s.split(',');
let len = str.length;
let rootVal = str.shift();
let root = new TreeNode(rootVal);
let stack = [];
let cur = root;
while(str.length || cur){
let i = 0;
while(cur){
stack.push(cur);
let left = str.shift();
if(left != '#'){
cur.left = new TreeNode(left);
}
cur = cur.left;
}
cur = stack.pop();
let right = str.shift();
if(right != '#'){
cur.right = new TreeNode(right);
}
cur = cur.right;
}
return root;
}
module.exports = {
Serialize : Serialize,
Deserialize : Deserialize
};
this.val = x;
this.left = null;
this.right = null;
}
function Serialize(pRoot)
{
// write code here
if(!pRoot){
return '';
}
let str = '';
function dfs(pRoot){
if(!pRoot){
str += ',#';
return;
}
str +=','+pRoot.val;
dfs(pRoot.left);
dfs(pRoot.right);
}
dfs(pRoot);
console.log(str);
return str.slice(1);
}
function Deserialize(s)
{
// write code here
if(s === ''){
return null;
}
let str = s.split(',');
let len = str.length;
let rootVal = str.shift();
let root = new TreeNode(rootVal);
let stack = [];
let cur = root;
while(str.length || cur){
let i = 0;
while(cur){
stack.push(cur);
let left = str.shift();
if(left != '#'){
cur.left = new TreeNode(left);
}
cur = cur.left;
}
cur = stack.pop();
let right = str.shift();
if(right != '#'){
cur.right = new TreeNode(right);
}
cur = cur.right;
}
return root;
}
module.exports = {
Serialize : Serialize,
Deserialize : Deserialize
};