码迷,mamicode.com
首页 > 其他好文 > 详细

03-树3 Tree Traversals Again (25分)

时间:2020-05-11 12:57:57      阅读:76      评论:0      收藏:0      [点我收藏+]

标签:uniq   ica   must   exist   先序   else   which   spec   gen   

An inorder binary tree traversal can be implemented in a non-recursive way with a stack. For example, suppose that when a 6-node binary tree (with the keys numbered from 1 to 6) is traversed, the stack operations are: push(1); push(2); push(3); pop(); pop(); push(4); pop(); pop(); push(5); push(6); pop(); pop(). Then a unique binary tree (shown in Figure 1) can be generated from this sequence of operations. Your task is to give the postorder traversal sequence of this tree.

技术图片
Figure 1

Input Specification:

Each input file contains one test case. For each case, the first line contains a positive integer N (≤) which is the total number of nodes in a tree (and hence the nodes are numbered from 1 to N). Then 2 lines follow, each describes a stack operation in the format: "Push X" where X is the index of the node being pushed onto the stack; or "Pop" meaning to pop one node from the stack.

Output Specification:

For each test case, print the postorder traversal sequence of the corresponding tree in one line. A solution is guaranteed to exist. All the numbers must be separated by exactly one space, and there must be no extra space at the end of the line.

Sample Input:

6
Push 1
Push 2
Push 3
Pop
Pop
Push 4
Pop
Pop
Push 5
Push 6
Pop
Pop
 

Sample Output:

3 4 2 6 5 1


解析:先建树,在后后序遍历
push的顺序就是先序遍历的顺序
pop的顺序就是中序的顺序
#include<iostream>
#include<stack>
#include<string>

using namespace std;


 int pre[40],in[40],post[40];//以此为先序,中序,后序的输入序列
int n;
   stack<int> st;//
 struct node
 {
     int data;
     node* left;
     node*  right;
 };

 node* create(int prel, int prer, int inl, int inr)//通过先序,中序建树
 {
     if(prel>prer)
        return NULL;
     node* root = new node;
     root ->data  = pre[prel];
     int k=0;
     for(k = inl;k<=inr;k++)
     {
         if(in[k]==pre[prel])
            break;
     }
     //k指示了中序遍历的根节点的位置
     int numleft = k - inl;

     root ->left = create(prel+1,prel+numleft,inl,k-1);
     root->right = create(prel+numleft+1,prer,k+1,inr);

     return root;

 }
 int num=0;
void postorder(node* root)//后序遍历
{
    if(root == NULL)
        return;
    postorder(root->left);
    postorder(root->right);
    printf("%d",root->data);
    num++;

    if(num<n)
        printf(" ");
}
int main()
{
   scanf("%d",&n);
   string str;



   int x, preindex=0, inindex=0;
   for(int i=0;i<2*n;i++)
   {
       cin>>str;
       if(!str.compare("Push"))//如果str和push相等,s.compare("Push")返回0       {
           scanf("%d",&x);
           pre[preindex++] = x;//输入先序遍历
            st.push(x);
       }
       else
       {
           int temp = st.top();
           st.pop();
           in[inindex++] = temp;//中序遍历
       }
   }

   node* root = create(0,n-1,0,n-1);
   postorder(root);

   return 0;

}

 

03-树3 Tree Traversals Again (25分)

标签:uniq   ica   must   exist   先序   else   which   spec   gen   

原文地址:https://www.cnblogs.com/qinmin/p/12868261.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!