zhedahht / CodingInterviewChinese2

《剑指Offer:名企面试官精讲典型编程面试题》第二版源代码
Other
5.32k stars 2.17k forks source link

Utilities目录中的Tree.cpp有一处错误 #8

Closed Fujihai closed 2 years ago

Fujihai commented 7 years ago

在PrintTreeNode(const TreeNode* pNode)函数中,遗漏了一句++i;,如下:

void PrintTreeNode(const TreeNode* pNode)
{
    if (pNode != nullptr)
    {
        printf("value of this node is: %d.\n", pNode->m_nValue);

        printf("its children is as the following:\n");
        std::vector<TreeNode*>::const_iterator i = pNode->m_vChildren.begin();
        while (i < pNode->m_vChildren.end())
        {
            if (*i != nullptr)
            {
                printf("%d\t", (*i)->m_nValue);
                ++i;       //此句不加会导致程序进入死循环
            }
            printf("%d\t", (*i)->m_nValue);
        }

        printf("\n");
    }
    else
    {
        printf("this node is nullptr.\n");
    }

    printf("\n");
}
middleprince commented 5 years ago

对,应该是作者在对存放子节点的vector遍历时忘记的了对迭代器递增。