前序:
void PreorderNoR(Node *root){
stack<Node *> s;
Node *cur = root;
while(!s.empty()&&cur!=NULL){
while(cur!=NULL){
printf("%c",cur->value);
s.push(cur);
cur = cur->left;
}
Node *top = s.top();
s.pop();
cur = top->right;
}
printf("\n");
}
中序:
void InorderNoR(Node *root){
stack<Node *> s;
Node *cur = root;
while(!s.empty()||cur!=NULL){
while(cur!=NULL){
s.push(cur);
cur = cur->left;
}
Node *top = s.top();
s.pop();
printf("%c",top->value);
cur = top->right;
}
printf("\n");
}
后序:
void Postorder(Node *root){
stack<Node *> s;
Node *cur = root;
Node *last = NULL;//上一次被完整遍历完的树的根结点
while(!s.empty()||cur!=NULL){
while(cur!=NULL)
{
s.push(cur);
cur = cur->left;
}
Node *top = s.top();
if(top->right == NULL){
printf("%c",top->value);
s.pop();
last = top;
}
else if(top->right == last){
printf("%c",top->value);
s.pop();
last = top;
}
else{
cur = top->right;
}
cur = top->right;
}
}