#include<iostream>
#include<stack>
using namespace std;
struct Node{
int data;
Node* lch;
Node* rch;
};
stack<Node*> s;
void preOrder(Node *root){
if(root==NULL)
return;
Node* node=root;
while(node!=NULL){
cout<<node->data<<endl;
if(node->lch!=NULL){
if(node->rch!=NULL)
s.push(node);
node=node->lch;
}
else{
if(node->rch!=NULL){
if(node==s.top())
s.pop();
node=node->rch;
}
else{
if(!s.empty()){
node=s.top();
s.pop();
node=node->rch;
}
else break;
}
}
}
}
二叉树的非递归前序遍历
最新推荐文章于 2022-11-05 20:35:17 发布