刚刚偶然看到了很早之前学习算法时照着书打的后序遍历的算法的草稿(非递归实现)
还是C++的呢,虽然现在早已不接触C++了,但是还是觉得很亲切。。。。哈哈哈哈哈!
struct StNode{
Node * node;
int TimesPop;
StNode(Node * N=null):node(N),TimesPop(0){}
};
template <class Type>
void BinaryTree <Type>::postOrder() const
{
linkStack<Node *> s;
StNode current(root);
cout<<"后序遍历:";
s.push(current);
while(!s.isEmpty()){
current=s.pop();
if(++current.TimesPop==3){
cout<<current.node->data;
continue;
}
p.push(current);
else if(current.TimesPop==1){
if(current.node->left!=null) s.push(StNode(current.node->left));
}
else {
if(current.node->right!=null) s.push(StNode(current.node->right));
}
}
}