w*x
3 楼
/*
Serialize/DeSerialize a tree
*/
struct NODE
{
int nVal;
vector vec;
NODE(int n) : nVal(n) {}
};
void _inner_serial(NODE* pNode, char*& p)
{
if (NULL == pNode)
return;
*p++ = pNode->vec.size();
*p++ = pNode->nVal;
for (vector::iterator it = pNode->vec.begin();
it != pNode->vec.end(); it++)
_inner_serial(*it, p);
}
const char* Serialize(NODE* pRoot, char mem[])
{
if (NULL == mem || NULL == pRoot)
return NULL;
char* p = mem;
_inner_serial(pRoot, p);
return mem;
}
NODE* _inner_deserial(const char*& p)
{
int n = *p++;
NODE* pRet = new NODE(*p++);
for (int i = 0; i < n; i++)
pRet->vec.push_back(_inner_deserial(p));
return pRet;
}
NODE* DeSerialize(const char mem[])
{
if (NULL == mem)
return NULL;
const char* p = mem;
return _inner_deserial(p);
}
Serialize/DeSerialize a tree
*/
struct NODE
{
int nVal;
vector
NODE(int n) : nVal(n) {}
};
void _inner_serial(NODE* pNode, char*& p)
{
if (NULL == pNode)
return;
*p++ = pNode->vec.size();
*p++ = pNode->nVal;
for (vector
it != pNode->vec.end(); it++)
_inner_serial(*it, p);
}
const char* Serialize(NODE* pRoot, char mem[])
{
if (NULL == mem || NULL == pRoot)
return NULL;
char* p = mem;
_inner_serial(pRoot, p);
return mem;
}
NODE* _inner_deserial(const char*& p)
{
int n = *p++;
NODE* pRet = new NODE(*p++);
for (int i = 0; i < n; i++)
pRet->vec.push_back(_inner_deserial(p));
return pRet;
}
NODE* DeSerialize(const char mem[])
{
if (NULL == mem)
return NULL;
const char* p = mem;
return _inner_deserial(p);
}
e*s
8 楼
再CAIWU的面经上看到的,有没有大牛给个IDEA?
w*x
10 楼
/*
Serialize/DeSerialize a tree
*/
struct NODE
{
int nVal;
vector vec;
NODE(int n) : nVal(n) {}
};
void _inner_serial(NODE* pNode, char*& p)
{
if (NULL == pNode)
return;
*p++ = pNode->vec.size();
*p++ = pNode->nVal;
for (vector::iterator it = pNode->vec.begin();
it != pNode->vec.end(); it++)
_inner_serial(*it, p);
}
const char* Serialize(NODE* pRoot, char mem[])
{
if (NULL == mem || NULL == pRoot)
return NULL;
char* p = mem;
_inner_serial(pRoot, p);
return mem;
}
NODE* _inner_deserial(const char*& p)
{
int n = *p++;
NODE* pRet = new NODE(*p++);
for (int i = 0; i < n; i++)
pRet->vec.push_back(_inner_deserial(p));
return pRet;
}
NODE* DeSerialize(const char mem[])
{
if (NULL == mem)
return NULL;
const char* p = mem;
return _inner_deserial(p);
}
Serialize/DeSerialize a tree
*/
struct NODE
{
int nVal;
vector
NODE(int n) : nVal(n) {}
};
void _inner_serial(NODE* pNode, char*& p)
{
if (NULL == pNode)
return;
*p++ = pNode->vec.size();
*p++ = pNode->nVal;
for (vector
it != pNode->vec.end(); it++)
_inner_serial(*it, p);
}
const char* Serialize(NODE* pRoot, char mem[])
{
if (NULL == mem || NULL == pRoot)
return NULL;
char* p = mem;
_inner_serial(pRoot, p);
return mem;
}
NODE* _inner_deserial(const char*& p)
{
int n = *p++;
NODE* pRet = new NODE(*p++);
for (int i = 0; i < n; i++)
pRet->vec.push_back(_inner_deserial(p));
return pRet;
}
NODE* DeSerialize(const char mem[])
{
if (NULL == mem)
return NULL;
const char* p = mem;
return _inner_deserial(p);
}
m*k
17 楼
抛一砖:
How about in-order traversal?
String serialize(Node root){
if(root == null){
return "()";
}
else{
return "(" + serialize( root.left) + root.val + serialize( root.
right) + ")" ;
}
}
ex:
1
/ \
2 3
/ \
4 5
will return
( ( (()4()) 2 ( ()5()) ) 1 ( ()3 () ) )
deserialize:
easy with stack,
How about in-order traversal?
String serialize(Node root){
if(root == null){
return "()";
}
else{
return "(" + serialize( root.left) + root.val + serialize( root.
right) + ")" ;
}
}
ex:
1
/ \
2 3
/ \
4 5
will return
( ( (()4()) 2 ( ()5()) ) 1 ( ()3 () ) )
deserialize:
easy with stack,
z*e
18 楼
做一个bfs就好了
((root))
(()(1)(2))
(()(3)()(4))
((root))
(()(1)(2))
(()(3)()(4))
相关阅读
很大的公司被更大的公司收购g家adwords express未来发展如何?Engineer negotiated $250k做 Reverse Osmosis 的童鞋请进!! (转载)国内已网友对外嫁的评价,有意思 -ZT (转载)如果你有合同工工作想找公司办H1B如果你有合同工工作想找公司办H1B求教:如何绕开老板,直接获取本行业的市场动态?一个公司没问身份问题就让我去onsite九章系统设计班面试求bless实现find的功能HR发面试邀请后消失了?今天听到公司一男一女两个老中在说话,那个女的说公司有一个full stack初级职位我说马工行业要衰退H1b延期咨询:合同从8月开始,延期会从5月开始吗?求data visualization的refer一道题目招聘市场推广人员