二叉树的遍历非递归

来源:互联网 发布:c语言嵌入式汇编 编辑:程序博客网 时间:2024/06/07 00:14

先序遍历(非递归):

void freoderIter(node* root){    stack<node*> s;    if(root==NULL)        return;    s.push(root);    while(!s.empty())    {        node *p,*q;        p=s.top();        cout<<p;        s.pop();        q=p->right;        if(q)            s.push(q);        q=p->lift;        if(q)            s.push(q);    }}
中序遍历:

void freoderIter(node* root){    stack<node*> s;    if(root==NULL)        return;    node *p=root;    while(!s.empty()||p)    {        while(p)        {             s.push(p);             p=p->left;        }        if(!s.empty())        {            p=s.top();            s.pop();            cout << p->data;            p=p->right;        }    }}





原创粉丝点击