1020. Tree Traversals (25)

102 篇文章 0 订阅
13 篇文章 0 订阅

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[N];
  inorder = new int[N];
  leftindex = new int[N];
  rightindex = new int[N];
  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;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值