题目:请实现两个函数,分别用来序列化和反序列化二叉树
序列化思路:可以根据前序遍历的思路来序列化二叉树
void Serialize(BinaryTreeNode *pRoot) {
if (NULL == pRoot) {
printf("$");
return;
}
printf("%d,",pRoot->m_nValue);
Serialize(pRoot->m_pLeft);
Serialize(pRoot->m_pRight);
}
反序列化思路:根据前序遍历的结果递归转换。
bool isnumber(char *nums, int *number) {
return true;
}
void Deserialize(BinaryTreeNode **pRoot,char *nums) {
int number;
if (true == isnumber(nums, &number)) {
*pRoot = (BinaryTreeNode *)calloc(1, sizeof(BinaryTreeNode));
(*pRoot)->m_nValue = number;
(*pRoot)->m_pLeft = NULL;
(*pRoot)->m_pRight = NULL;
}
Deserialize(&(*pRoot)->m_pLeft,nums);
Deserialize(&(*pRoot)->m_pRight,nums);
}