PAT:1086. Tree Traversals Again (25) AC

#include<stdio.h>
#include<string.h>
#include<stack>
using namespace std;
const int MAX=50;

int n,cnt=0;      //n个节点,cnt在后序输出的时候控制空格数量用
int PRE[MAX],IN[MAX];  //先序,中序
int preI,inI;      //先序,中序的下标
stack<int> S;      //栈

struct node
{
  int data;
  node* l;
  node* r;
};

node* create(int preL,int preR,int inL,int inR)
{
  if(preL>preR)
    return NULL;
  node* root=new node;
  root->data=PRE[preL];
  root->l=NULL;
  root->r=NULL;
  int k;
  for(k=inL ; k<=inR ; ++k)    //在中序找到这个节点
    if(IN[k]==root->data)
      break;
  int numleft=k-inL;        //此节点左子树个数
  root->l=create(preL+1,preL+numleft,inL,k-1);  //【caution】下一轮先序下标到preL+numleft。
  root->r=create(preL+numleft+1,preR,k+1,inR);
  return root;
}

void postorder(node* root)
{
  if(root==NULL)
    return;
  postorder(root->l);
  postorder(root->r);
  printf("%d",root->data);
  ++cnt;
  if(cnt<n)
    printf(" ");
}

int main()
{
  preI=inI=0;
  scanf("%d",&n);
  for(int i=0 ; i<2*n ; ++i)    //【思维】Push顺序刚好是先序序列,Pop序列刚好是中序序列,可以建立二叉树,然后后序遍历
  {
    char tmp[5];        //最多为Push,4个char但是\0占一个,至少申请5个char空间
    int num;          //暂存Push和Pop数
    scanf("%s",tmp);
    if(strcmp(tmp,"Push")==0)  //Push则进栈
    {
      scanf("%d",&num);
      S.push(num);
      PRE[preI++]=num;    //num也是先序序列数
    }
    else
    {
      num=S.top();      //Pop出栈
      S.pop();
      IN[inI++]=num;      //num是中序序列数
    }
  }
  node* root=create(0,inI-1,0,preI-1);
  postorder(root);
  return 0;
}
时间: 2024-10-12 12:12:04

PAT:1086. Tree Traversals Again (25) AC的相关文章

PAT Advanced 1086 Tree Traversals Again (25分)

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()

PAT Advanced 1086 Tree Traversals Again (25) [树的遍历]

题目 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); po

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

题目信息 1086. Tree Traversals Again (25) 时间限制200 ms 内存限制65536 kB 代码长度限制16000 B 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

PAT:1020. Tree Traversals (25) AC

#include<stdio.h> #include<stdlib.h> #include<queue> using namespace std; int POST[32]; //存放后序遍历 int IN[32]; //存放中序遍历 int n; //节点数 struct node { int data; node* l; node* r; }; node* creat(int postL,int postR,int inL,int inR) { if(postL&g

PAT 1086 Tree Traversals Again (25)

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()

PAT (Advanced Level) 1086. Tree Traversals Again (25)

入栈顺序为先序遍历,出栈顺序为中序遍历. #include<cstdio> #include<cstring> #include<cmath> #include<algorithm> #include<stack> using namespace std; const int maxn=1000+10; const int INF=0x7FFFFFFF; int n,tot; int Preorder[maxn],Inorder[maxn],Po

1086. Tree Traversals Again (25)

时间限制 200 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue push 的顺序就是二叉树的前序 pop的顺序就是二叉树的中序遍历 本质上还是考根据这两个顺序建立二叉树,并且进行后序遍历 An inorder binary tree traversal can be implemented in a non-recursive way with a stack. For example, suppose that when

PAT 甲级 1086 Tree Traversals Again

https://pintia.cn/problem-sets/994805342720868352/problems/994805380754817024 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

PAT:1060. Are They Equal (25) AC

#include<iostream> #include<string> #include<vector> #include<algorithm> using namespace std; int n; string deal(string s,int &e) //[思维]1:吸收多余0:.2:找到“.”判断与1的大小.3:去除“.”的同时统计10的指数(正负与step2有关) { //4:判断是否删完了,删完了表明数字是0.5:存入前n个数字,不足用