二叉树前序遍历的非递归实现(京东笔试考过)

来源:互联网 发布:中国电信cn2网络 编辑:程序博客网 时间:2024/05/01 06:28
void PreOrder(BiTree root){    stack<BiTree> bitreeStack;        while(root!=NULL || !bitreeStack.empty())    {        while(root!=NULL)        {            cout<<root->data<<endl;            bitreeStack.push(root);            root=root->lchild;            }        if(!bitreeStack.empty())        {            root=bitreeStack.top();            bitreeStack.pop();            root=root->rchild;            }        }    }

0 0
原创粉丝点击