03-树3 Tree Traversals Again(25 分)

0人阅读 评论(0) 收藏 举报
分类:

题目来源:中国大学MOOC-陈越、何钦铭-数据结构-2018春
作者: 陈越
单位: 浙江大学

问题描述:
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 (≤30) which is the total number of nodes in a tree (and hence the nodes are numbered from 1 to N). Then 2N 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

解答:压入栈的顺序是前序序列,弹栈的顺序是中序序列,问题转化为,给出前序中序确定一棵树,并后序输出。建树过程主要就是,前序序列的最左边即为根节点,找到中序序列中的根节点,中序根节点以左为左子树,以右为右子树,递归建树即可。

#include <iostream>
#include <stack>
#include <string>
#include <vector>
using namespace std;
const int maxn=30;
int N,inorder[maxn],preorder[maxn];
stack<int> tempStack;
vector<int> postorder;
struct node
{
    int data;
    node* leftChild;
    node* rightChild;
};
node* buildTree(int preL,int preR,int inL,int inR)
{
    node* root=new node();
    if(preL>preR)
        return NULL;
    root->data=inorder[inL];
    //寻找前序中的根节点
    int rootPos,len;
    for(int i=preL;i<=preR;i++)
    {
        if(inorder[inL]==preorder[i])
        {
            rootPos=i;
            break;
        }
    }
    len=rootPos-preL;
    root->leftChild=buildTree(preL,rootPos-1,inL+1,inL+len);
    root->rightChild=buildTree(rootPos+1,preR,inL+len+1,inR);
    return root;
}
void input()
{
    string in;
    int inorderPos=0;
    int preorderPos=0;
    for(int i=0;i<2*N;i++)
    {
        cin>>in;
        if(in[1]=='u')
        {
            int num;
            cin>>num;
            inorder[inorderPos]=num;
            tempStack.push(num);
            inorderPos++;
        }
        else if(in[1]=='o')
        {
            int num=tempStack.top();
            tempStack.pop();
            preorder[preorderPos]=num;
            preorderPos++;
        }
    }
}
void postorderTravel(node* root)
{

    if(root!=NULL)
    {
        postorderTravel(root->leftChild);
        postorderTravel(root->rightChild);
        postorder.push_back(root->data);
    }
}
void output()
{
    for(int i=0;i<postorder.size();i++)
    {
        if(i==0)
            cout<<postorder[i];
        else
            cout<<" "<<postorder[i];
    }
}
int main()
{
    cin>>N;
    input();
    node* tree=buildTree(0,N-1,0,N-1);
    postorderTravel(tree);
    output();
    return 0;
}
查看评论

03-树3. Tree Traversals Again (25)

问题描述: 输入显示的是中序遍历非递归,栈的操作,输出树的后序遍历 问题关键 push的顺序为先序遍历,pop的顺序为中序遍历 问题转换为由先序遍历、中序遍历求后续遍历/*==========...
  • whzyb1991
  • whzyb1991
  • 2015-07-01 13:01:21
  • 1841

03-树3 Tree Traversals Again (25分)

03-树3 Tree Traversals Again (25分)An inorder binary tree traversal can be implemented in a non-recu...
  • shinanhualiu
  • shinanhualiu
  • 2015-10-20 16:30:24
  • 1712

Tree Traversals Again (25)

An inorder binary tree traversal can be implemented in a non-recursive way with a stack. For example...
  • qinboxiang232
  • qinboxiang232
  • 2015-03-30 18:36:16
  • 199

03-树3 Tree Traversals Again   (25分)

/*===================================================================================== # COPY...
  • The__Apollo
  • The__Apollo
  • 2016-11-30 13:32:22
  • 283

03-树3. Tree Traversals Again

An inorder binary tree traversal can be implemented in a non-recursive way with a stack. For example...
  • lulu901130
  • lulu901130
  • 2015-03-28 15:23:16
  • 282

03-树3 Tree Traversals Again

#include #include #include #define MAXSIZE 30 typedef struct node{ int top; int cap; int dat...
  • asunamashiro
  • asunamashiro
  • 2015-09-22 17:18:14
  • 613

Tree Traversals Again(mooc 浙大算法课)

03-树3 Tree Traversals Again (25分) An inorder binary tree traversal can be implemented in a non-re...
  • tdscdma123
  • tdscdma123
  • 2016-09-22 09:26:50
  • 1029

PAT - 03-树2 List Leaves (25分)

03-树2 List Leaves   (25分) Given a tree, you are supposed to list all the leaves in the order ...
  • flx413
  • flx413
  • 2016-09-21 12:43:55
  • 679

1086. Tree Traversals Again (25)【二叉树】——PAT (Advanced Level) Practise

题目信息1086. Tree Traversals Again (25)时间限制200 ms 内存限制65536 kB 代码长度限制16000 B An inorder binary tree ...
  • xianyun2009
  • xianyun2009
  • 2016-03-19 01:12:26
  • 1355

1020. Tree Traversals (25) -BFS

这是一道很直接的给出中序序列和任一其他序列生成二叉树的问题,本题给出的是后序遍历和中序遍历,利用后序遍历的“左右根”顺序我们知道,后序序列的最后一个元素一定是整棵树的根,从后向前,分别是右、左子树的根...
  • xyt8023y
  • xyt8023y
  • 2015-05-30 13:44:40
  • 1430
    个人资料
    持之以恒
    等级:
    访问量: 637
    积分: 290
    排名: 27万+
    文章存档