二叉树中序非递归算法的思路是:
- push节点,移往左子树,直到坐子树为空
- pop节点,移往右子树。
typedef struct node
{
char data;
struct node *lchild, *rchild;
}BinTree;
void inOrder(BinTree * root)
{
stack<BinTree*> s;
BinTree *p = root;
while(p!=NULL || !s.empty())
{
while(p != NULL)
{
s.push(p);
p = p->lchild;
}
if(!s.empty())
{
p = s.top();
cout<<p->data<<" ";
s.pop();
p = p->rchild;
}
}
}