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>postR)    //表明后续已经遍历完了
    return NULL;
  node* root=(node*)calloc(1,sizeof(node));    //或者写成  node* root=new node;  而calloc会自动初始化root更方便
  root->data=POST[postR];
  root->l=NULL;
  root->r=NULL;
  int tmp=0;      //标记postR这个元素在中序遍历下的位置
  for(tmp=inL ; tmp<=inR ; ++tmp)
    if(IN[tmp]==root->data)
      break;
  int numleft=tmp-inL;  //【思维】后序遍历中,前numleft个数字都是后续根的左子树
  root->l=creat(postL, postL+numleft-1, inL, tmp-1);    //【caution】这里的下标千万注意!!!
  root->r=creat(postL+numleft , postR-1 , tmp+1 , inR);  //写这步的时候最好画图
  return root;    //返回根地址
}

void BFS(node* root)
{
  queue<node*> q;    //创建队列
  q.push(root);
  int num=0;      //记录输出个数,控制空格数量
  while(!q.empty())
  {
    ++num;
    node* tmp=q.front();
    q.pop();
    printf("%d",tmp->data);
    if(num<n)
      printf(" ");
    if(tmp->l!=NULL)
      q.push(tmp->l);
    if(tmp->r!=NULL)
      q.push(tmp->r);
  }
}

void DFS(node* root)
{
  if(root==NULL)
    return;
  printf("%d ",root->data);
  DFS(root->l);
  DFS(root->r);
}

int main()
{
  scanf("%d",&n);
  for(int i=0 ; i<n ; ++i)
    scanf("%d",&POST[i]);
  for(int i=0 ; i<n ; ++i)
    scanf("%d",&IN[i]);
  node* root=creat(0,n-1,0,n-1);

  //DFS(root);    //先序遍历,测试建树情况

  BFS(root);

  return 0;
}
时间: 2024-10-14 22:15:45

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

【PAT】1020 Tree Traversals (25)(25 分)

1020 Tree Traversals (25)(25 分) Suppose that all the keys in a binary tree are distinct positive integers. Given the postorder and inorder traversal sequences, you are supposed to output the level order traversal sequence of the corresponding binary

PAT Advanced 1020 Tree Traversals (25分)

1020 Tree Traversals (25分) Suppose that all the keys in a binary tree are distinct positive integers. Given the postorder and inorder traversal sequences, you are supposed to output the level order traversal sequence of the corresponding binary tree.

PAT 1020. Tree Traversals (25)

1020. Tree Traversals (25) Suppose that all the keys in a binary tree are distinct positive integers. Given the postorder and inorder traversal sequences, you are supposed to output the level order traversal sequence of the corresponding binary tree.

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; nod

1020. Tree Traversals (25) PAT甲级真题

之前我看了这道题,实在是看不懂网上的解题答案,他们的具体思路基本上就是通过后续遍历和中序遍历,直接推出层次遍历. 我苦思冥想了半天,是在没看懂这种思路,于是想了一个笨点的但是也比较好理解的思路,通过后续和中序,先推出整个二叉树,再考虑 对二叉树层次遍历. 本题还有一点要注意的时在输出结果的末尾,如果使用了类似 pirntf("%d ",data); 这样的格式是不对的,一定要对末尾进行判断消除最尾端的空格. 首先最核心的部分是通过两次遍历反推回二叉树:这里的思路是,后续遍历的最末尾,一

浙大pat甲级题目---1020. Tree Traversals (25)

Suppose that all the keys in a binary tree are distinct positive integers. Given the postorder and inorder traversal sequences, you are supposed to output the level order traversal sequence of the corresponding binary tree. Input Specification: Each

PAT (Advanced Level) 1020. Tree Traversals (25)

递归建树,然后BFS一下 #include<iostream> #include<cstring> #include<cmath> #include<algorithm> #include<cstdio> #include<queue> #include<vector> using namespace std; const int maxn=40; int a[maxn],b[maxn]; int n,tot; struc

1020. Tree Traversals (25)

根据两种遍历方式建立二叉树 层遍历二叉树 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue Suppose that all the keys in a binary tree are distinct positive integers. Given the postorder and inorder traversal sequences, you are supposed to output the le

1020 Tree Traversals (25 分)

Suppose that all the keys in a binary tree are distinct positive integers. Given the postorder and inorder traversal sequences, you are supposed to output the level order traversal sequence of the corresponding binary tree. Input Specification: Each