由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
JobHunting版 - 一个树,不一定是2叉树,让设计一个数据结构来serialize和 deserialize
相关主题
发几个面试题谷歌面经
几道F家面试题F家phone interview的一道题
刚才重新回顾了一下那题F家面经
binary tree的in-order iterator怎么写?binary tree的 serialization
贡献一道G家的onsite题和简单面经(已悲剧)做了一下merge BST
很可能被这题搞挂了,sort linked listFB面试题:binary tree inorder successor
请问如何sort一个linked list?问道G家的面试题。
求个java版本的binary tree serialization和deserializationG题,把binary tree里面的sibling节点连接起来
相关话题的讨论汇总
话题: node话题: null话题: pnode话题: serialize话题: mem
进入JobHunting版参与讨论
1 (共1页)
e***s
发帖数: 799
1
再CAIWU的面经上看到的,有没有大牛给个IDEA?
j*****y
发帖数: 1071
2
感觉还是可以用 pre-order travers 一个 left child, right siblin 的结构吧.

【在 e***s 的大作中提到】
: 再CAIWU的面经上看到的,有没有大牛给个IDEA?
w****x
发帖数: 2483
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);
}
j*****y
发帖数: 1071
4
thanks.

【在 w****x 的大作中提到】
: /*
: Serialize/DeSerialize a tree
: */
: struct NODE
: {
: int nVal;
: vector vec;
: NODE(int n) : nVal(n) {}
: };
: void _inner_serial(NODE* pNode, char*& p)

j*****y
发帖数: 1071
5
这种情况下 child是没有顺序的,是吧?
如何把 serialized的 array写到一个file去,然后从file读回来 重建树呢?
多谢 :)

【在 w****x 的大作中提到】
: /*
: Serialize/DeSerialize a tree
: */
: struct NODE
: {
: int nVal;
: vector vec;
: NODE(int n) : nVal(n) {}
: };
: void _inner_serial(NODE* pNode, char*& p)

c***w
发帖数: 134
6

我一直对你的头像很感兴趣,这个女的是谁?

【在 w****x 的大作中提到】
: /*
: Serialize/DeSerialize a tree
: */
: struct NODE
: {
: int nVal;
: vector vec;
: NODE(int n) : nVal(n) {}
: };
: void _inner_serial(NODE* pNode, char*& p)

e***s
发帖数: 799
7
非常感谢!!

【在 w****x 的大作中提到】
: /*
: Serialize/DeSerialize a tree
: */
: struct NODE
: {
: int nVal;
: vector vec;
: NODE(int n) : nVal(n) {}
: };
: void _inner_serial(NODE* pNode, char*& p)

e***s
发帖数: 799
8
再CAIWU的面经上看到的,有没有大牛给个IDEA?
j*****y
发帖数: 1071
9
感觉还是可以用 pre-order travers 一个 left child, right siblin 的结构吧.

【在 e***s 的大作中提到】
: 再CAIWU的面经上看到的,有没有大牛给个IDEA?
w****x
发帖数: 2483
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);
}
相关主题
很可能被这题搞挂了,sort linked list谷歌面经
请问如何sort一个linked list?F家phone interview的一道题
求个java版本的binary tree serialization和deserializationF家面经
进入JobHunting版参与讨论
j*****y
发帖数: 1071
11
thanks.

【在 w****x 的大作中提到】
: /*
: Serialize/DeSerialize a tree
: */
: struct NODE
: {
: int nVal;
: vector vec;
: NODE(int n) : nVal(n) {}
: };
: void _inner_serial(NODE* pNode, char*& p)

j*****y
发帖数: 1071
12
这种情况下 child是没有顺序的,是吧?
如何把 serialized的 array写到一个file去,然后从file读回来 重建树呢?
多谢 :)

【在 w****x 的大作中提到】
: /*
: Serialize/DeSerialize a tree
: */
: struct NODE
: {
: int nVal;
: vector vec;
: NODE(int n) : nVal(n) {}
: };
: void _inner_serial(NODE* pNode, char*& p)

c***w
发帖数: 134
13

我一直对你的头像很感兴趣,这个女的是谁?

【在 w****x 的大作中提到】
: /*
: Serialize/DeSerialize a tree
: */
: struct NODE
: {
: int nVal;
: vector vec;
: NODE(int n) : nVal(n) {}
: };
: void _inner_serial(NODE* pNode, char*& p)

e***s
发帖数: 799
14
非常感谢!!

【在 w****x 的大作中提到】
: /*
: Serialize/DeSerialize a tree
: */
: struct NODE
: {
: int nVal;
: vector vec;
: NODE(int n) : nVal(n) {}
: };
: void _inner_serial(NODE* pNode, char*& p)

u*****o
发帖数: 1224
15
是赵雅芝啊,好像是她在京华烟云里姚木兰的扮相。。

【在 c***w 的大作中提到】
:
: 我一直对你的头像很感兴趣,这个女的是谁?

S********t
发帖数: 3431
16
you can encode the structure of the generic tree with 2n bits, and
reconstruct the structure from the 2n bits

【在 e***s 的大作中提到】
: 再CAIWU的面经上看到的,有没有大牛给个IDEA?
m*****k
发帖数: 731
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,
z****e
发帖数: 54598
18
做一个bfs就好了
((root))
(()(1)(2))
(()(3)()(4))
1 (共1页)
进入JobHunting版参与讨论
相关主题
G题,把binary tree里面的sibling节点连接起来贡献一道G家的onsite题和简单面经(已悲剧)
攒人品,Twitter 电面题目很可能被这题搞挂了,sort linked list
今天面的太惨了....请问如何sort一个linked list?
bst中序遍历c++ class iterator求个java版本的binary tree serialization和deserialization
发几个面试题谷歌面经
几道F家面试题F家phone interview的一道题
刚才重新回顾了一下那题F家面经
binary tree的in-order iterator怎么写?binary tree的 serialization
相关话题的讨论汇总
话题: node话题: null话题: pnode话题: serialize话题: mem