void BFSLayer(BinaryTreeNode* pRoot) { if(pRoot==nullptr) return; queue<BinaryTreeNode*> pNode; int curLayer=1,nextLayer=0; pNode.push(pRoot); while(!pNode.empty()) { BinaryTreeNode* pFront=pNode.front(); cout<<pFront->m_Value<<' '; pNode.pop(); curLayer--; if(pFront->m_pLeft!=nullptr) { nextLayer++; pNode.push(pFront->m_pLeft); } if(pFront->m_pRight!=nullptr) { nextLayer++; pNode.push(pFront->m_pRight); } if(curLayer==0) { curLayer=nextLayer; nextLayer=0; cout<<endl; } } }
相关文章: