1086. Tree Traversals Again (25)

 



1086. Tree Traversals Again (25)

时间限制
200 ms
内存限制
65536 kB
代码长度限制
16000 B
判题程序
Standard
作者
CHEN, Yue

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.

1086. Tree Traversals Again (25) - 嫒mei贝 - ⊙-→棒棒糖ing 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
和http://blog.csdn.net/u014646950/article/details/47137173差不多;左边连接的是已知后序,中序,求水平层遍历;
而这一题入栈push的顺序是先序,而pop出来的顺序是中序,这里要求的是后序;
先序 中-左-右 
中序 左-中-右
            先序的头为当前子树的根,接着在中序中找到根在中序的位置,可以划分为【左子树】和【右子树】然后进行继续如此;知道左右子树都不能划分了

  
  

评测结果

时间结果得分题目语言用时(ms)内存(kB)用户
8月17日 23:13答案正确251086C++ (g++ 4.7.2)1308datrilla

测试点

测试点结果用时(ms)内存(kB)得分/满分
0答案正确130012/12
1答案正确13084/4
2答案正确11804/4
3答案正确13001/1
4答案正确11804/4
#include<iostream>
#include<vector> 
#include<stack>
using namespace std; 
void createT(vector<int>*pre, vector<int>*inor, vector<pair<int, int>>*Tree, int starindex, int endindex, int rootindex)
{ 
  int index = starindex;
  int root = (*pre)[rootindex];
  bool Flag = true;
  for (; index <= endindex&&Flag; index++)
  if( (*inor)[index]==root)
  {
    if (starindex < index)
    {
      (*Tree)[root].first = (*pre)[rootindex + 1];
      createT(pre, inor, Tree, starindex, index-1, rootindex+1);
    }
    if (index < endindex)
    {  
      (*Tree)[root].second = (*pre)[index-starindex+rootindex+1];
      createT(pre, inor, Tree, index + 1, endindex, index - starindex + rootindex+1);
    }
    Flag = false;
    
  } 
}
void rightFirstDFS(vector<pair<int, int>>*Tree, int root,int *firstflag)
{
  if ((*Tree)[root].first)
    rightFirstDFS(Tree, (*Tree)[root].first,firstflag);
  if ((*Tree)[root].second) 
    rightFirstDFS(Tree, (*Tree)[root].second ,firstflag);
  if (*firstflag == 0)
  {
    cout << root;
    *firstflag = 1;
  }
  else cout << " " << root;

}
int main()
{
  int N;
  int node;
  char order[5];
  cin >> N;
  vector<pair<int,int>>Tree(N+1);
  vector<int>pre;
  vector<int>inor;
  stack<int>sta;
  int index = 2 * N; 
  while (index--)
  {
    cin >> order;
    switch (order[1])
    {
    case 'u':
      cin>>node;   
      pre.push_back(node);
      sta.push(node);
      break;
    case 'o':
      inor.push_back(sta.top());  
      sta.pop();
      break;
    } 
  } 
  createT(&pre, &inor, &Tree, 0, N - 1, 0);
  node = 0; 
  rightFirstDFS(&Tree, pre[0],&node); 
  cout << endl;
  system("pause");
  return 0;
}


 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值