void CMap::DepthFirstTraversal(int NodeIndex) { if (m_pNode[NodeIndex].m_bIsVisited == false) { cout << m_pNode[NodeIndex].m_iData << " "; } m_pNode[NodeIndex].m_bIsVisited = true; for (int i = 0; i <m_iCapacity;i++) { if (1==GetvalueOfMatrix(NodeIndex, i)) { DepthFirstTraversal(i); } } } 上面这种写法就会导致内存错误 void CMap::DepthFirstTraversal(int NodeIndex) { cout << m_pNode[NodeIndex].m_iData << " "; m_pNode[NodeIndex].m_bIsVisited = true; for (int i = 0; i <m_iCapacity;i++) { if (1==GetvalueOfMatrix(NodeIndex, i)) { if (m_pNode[i].m_bIsVisited == false) { DepthFirstTraversal(i); } } } } 而这种写法就是正确的呢,按理来说,在递归前跟递归后判断是没啥区别的啊
朋友,实测你的代码没问题啊!