您的位置:首页 > 其它

1020. Tree Traversals (25)

2015-07-29 23:25 351 查看

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 level order traversal sequence of the corresponding binary tree.Input Specification:Each input file contains one test case. For each case, the first line gives a positive integer N (<=30), the total number of nodes in the binary tree. The second line gives the postorder sequence and the third line gives the inorder sequence. All the numbers in a line are separated by a space.Output Specification:For each test case, print in one line the level order traversal sequence of the corresponding binary tree. All the numbers in a line must be separated by exactly one space, and there must be no extra space at the end of the line.Sample Input:7 2 3 1 5 7 6 4 1 2 3 4 5 6 7
Sample Output:
4 1 6 3 5 7 2
N个结点
后序
中序
reaDlnAndCLEAR(int*postorder, int N, int*inorder,int*leftindex,int*rightindex)读入后序、中序 、并初始化假如在中序的对应序号N的左右都没有结点设为-1;
BinaryTree(int*postorder, int*roOt, int*inorder, int*leftindex, int*rightindex,int Star,int END)DFS深度优先搜索,后序的*roOt不断的减少,在中序中找到*roOt,看在此次的中序是否有右子树,有先右边,再看是否有左子树,有则左边
(后序 左-右-中,所以显然每次调用此函数
如果 左-nowroot-右 ,那么(*root)=nowroot-1是nowroot的右子,且当nowroot的右子数调用完此函数后,此时(*root)才是nowroot的左子;
如果 nowroot-右,那么(*root)=nowroot-1是nowroot的右子。
如果 左-nowroot (*root)=nowroot-1是nowroot的左子)
GetIT(int*postorder, int N, int*inorder, int*leftindex, int*rightindex) BFS广度优先搜索,这里我没有用queue,直接那inorder来充当queue的角色;

评测结果

时间结果得分题目语言用时(ms)内存(kB)用户
7月29日 22:59答案正确251020C++ (g++ 4.7.2)1308datrilla

测试点

测试点结果用时(ms)内存(kB)得分/满分
0答案正确130015/15
1答案正确13081/1
2答案正确12522/2
3答案正确13082/2
4答案正确11802/2
5答案正确12483/3
#include<iostream>     using namespace std;  void reaDlnAndCLEAR(int*postorder, int N, int*inorder,int*leftindex,int*rightindex){  int index;  for (index = 0; index < N; index++)    cin >> postorder[index];  for (index = 0; index < N; index++)  {    cin >> inorder[index];    leftindex[index] = -1;    rightindex[index] = -1;  } }void BinaryTree(int*postorder, int*roOt, int*inorder, int*leftindex, int*rightindex,int Star,int END){  int index,now,nowroot;  bool Flag = true;  now = postorder[(*roOt)];  nowroot = (*roOt)--;  for (index = Star; index < END&&Flag; index++)    if (now == inorder[index])    {      Flag = false;      if (index !=END - 1)      {         rightindex[nowroot] = (*roOt);          BinaryTree(postorder, roOt, inorder, leftindex, rightindex, index+1, END);       }      if (index != Star)      {        leftindex[nowroot] = (*roOt);           BinaryTree(postorder, roOt, inorder, leftindex, rightindex, Star, index);      }    }}void GetIT(int*postorder, int N, int*inorder, int*leftindex, int*rightindex){  int index,frontIndex,t;  frontIndex = 0;  index = 1;  inorder[0] = N-1;  while (index < N)  {       for (t = index; frontIndex < index; frontIndex++)    {       if (leftindex[inorder[frontIndex]] != -1) {        inorder[t++] = leftindex[inorder[frontIndex]];       }      if (rightindex[inorder[frontIndex]] != -1)      {        inorder[t++] = rightindex[inorder[frontIndex]];        }    }    frontIndex = index;    index = t;  }}void Display(int*postorder, int N, int*inorder){  int index = 0;  for (; index < N - 1; index++)    cout << postorder[inorder[index]] << " ";  cout << postorder[inorder[index]] << endl;}int main(){   int N;  int root;  int *postorder;  int *inorder;  int*leftindex;  int*rightindex;  cin >> N;  postorder = new int;  inorder = new int;  leftindex = new int;  rightindex = new int;  root = N-1;  reaDlnAndCLEAR(postorder, N, inorder, leftindex, rightindex);  BinaryTree(postorder,&root,inorder,leftindex, rightindex,0,N);  GetIT(postorder, N, inorder, leftindex, rightindex);  Display(postorder,N,inorder);  system("pause");    delete[]rightindex;  delete[]leftindex;  delete[]postorder;  delete[]inorder;  return 0;}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: