由于虚空无法返回任何内容,我不知道如何为我想要获得的虚函数获得正确的基本情况。
struct TreeNode {
char value;
TreeNode *sibling;
TreeNode *child;
};
void serialize(std::ostream &out, TreeNode *root)
{
// If the root is nullptr, print "None"
if (root == nullptr)
out << "None" << "\n";
// Write out root's value
out << root->value << "\n";
// if there is no child
// write out "False"
// else
// write out "True"
// recursively call serialize on that child
if (root->child == nullptr)
out << false << "\n";
else
{
out << true << "\n";
serialize(out, root->child);
}
// recursively call serialize on the sibling
serialize(out, root->sibling);
}
如果我将序列化重写为TreeNode类型函数会不会有帮助,如果我这样做会是什么基础?
注意:这是项目中用于在c ++中创建树节点数据结构的一个函数。
答案 0 :(得分:-2)
在此代码中,您尝试递归调用serialize函数,但没有指定终止条件。因此每次递归函数调用堆栈内存被占用时,最终导致堆栈溢出。像return语句一样添加终止点,它应该可以正常工作。
if (root == nullptr)
out << "None" << "\n";
return;