Tree - 1005
找 BUG
template <typename T>
void levelTraversal(BinaryNode<T>* root, void (*visit)(T &x))
{
if(root) {
stack<BinaryNode<T>*> nodeQueue;
stack<BinaryNode<T>*> a;
nodeQueue.push(root);
int k = 0;
while(k < nodeQueue.size())
{
BinaryNode<T>* temp;
for(int j = nodeQueue.size() - 1 - k; j > 0; --j) {
BinaryNode<T>* nodePop = nodeQueue.top();
nodeQueue.pop();
a.push(nodePop);
}
temp = nodeQueue.top();
for(int j = 0; j < a.size(); ++j) {
BinaryNode<T>* nodePop = a.top();
a.pop();
nodeQueue.push(nodePop);
}
++k;
visit(temp->elem);
if(temp->left)
nodeQueue.push(temp->left);
if(temp->right)
nodeQueue.push(temp->right);
}
}
}