template<class T>//中序遍历递归算法
void InOrderN(BTNode<T>* t)
{
if(!t)
return;
stack<BTNode<T>*> s;
while(t||!s.empty())
{
if(!t)
{
BTNode<T>* a;
a=s.top();
s.pop();
cout<<a->data;
t=a->right;
}
if(t)
{
s.push(t);
t=t->left;
}
}
}
template<class T>//前序遍历递归算法
void PreOrderN(BTNode<T>* t)
{
if(!t)
return;
stack<BTNode<T>*> s;
while(t||!s.empty())
{
if(t)
{
cout<<t->data;
s.push(t->right);
t=t->left;
}
else
{
t=s.top();
s.pop();
}
}
}
其实二叉树的深度优先遍历就是DFS,二叉树的层序优先遍历就是BFS。需要我们细心体会。