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))
相关阅读
最近有材料/化工的面试Du Pont吗?CS 4年学的东西有用还是EE 4年学的东西有用?做Java框架的,用spring,sturts,Hibernate的工作好找吗?报个amazon offer,求指点感觉学ee的现在很酸啊这两个offer难选。同学们来帮忙解个题吧~请问这是为什么请问大家关于h1b post职位找median有O(N)的算法吗?Senior System Engineer position in midtown NYC (转载)Help! H1b receipt没到,quit公司,还可以合法在美国吗一个不错的动态规划的教程PwC advisory-operation consulting 怎么样Does anyone use Excel for data analysis?教职和码工工作求比较!一般挑几个有兴趣的职位给HR比较合适今天面了个小公司,试试手。请问如果还未开始工作,H1-B申请的Paystubs如何得到?这是说明我没戏了吗?