关闭

PAT 甲级 1020 Tree Traversals (二叉树遍历)

标签: PAT 1020Tree Traversals二叉树
364人阅读 评论(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


根据后续遍历和中序遍历,求二叉树

#include <iostream>
#include <string.h>
#include <stdlib.h>
#include <algorithm>
#include <math.h>
#include <stdio.h>
#include <queue>

using namespace std;
typedef struct Tree
{
    int data;
    Tree *lchild;
    Tree *rchild;
}a[40];
int post[40];
int in[40];
int n;
int ans[40];
void dfs(int l1,int r1,int l2,int r2,Tree* &root)
{
  root=new Tree();
    int i;
    for( i=l1;i<=r1;i++)
        if(in[i]==post[r2])
            break;
    root->data=post[r2];
    if(i==l1)
        root->lchild=NULL;
    else
        dfs(l1,i-1,l2,l2+i-l1-1,root->lchild);
    if(i==r1)
        root->rchild=NULL;
    else
        dfs(i+1,r1,r2-(r1-i),r2-1,root->rchild);
}
int cnt;
void bfs(Tree *tree)
{
  queue<Tree*> q;
  q.push(tree);
  while(!q.empty())
  {
    Tree *root=q.front();
    q.pop();
    ans[cnt++]=root->data;
    if(root->lchild!=NULL)
      q.push(root->lchild);
    if(root->rchild!=NULL)
      q.push(root->rchild);
  }
}
int main()
{
    while(scanf("%d",&n)!=EOF)
    {
        for(int i=1;i<=n;i++)
            scanf("%d",&post[i]);
        for(int i=1;i<=n;i++)
            scanf("%d",&in[i]);
        Tree *tree;
        dfs(1,n,1,n,tree);
    cnt=0;
    bfs(tree);
    for(int i=0;i<cnt;i++)
    {
      if(i==cnt-1)
      printf("%d\n",ans[i]);
      else
        printf("%d ",ans[i]);
    }
    }
    return 0;
}


0
0
查看评论
发表评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场

【LeetCode-面试算法经典-Java实现】【103-Binary Tree Zigzag Level Order Traversal(二叉树分层Z字形遍历)】

【103-Binary Tree Zigzag Level Order Traversal(二叉树分层Z字形遍历)】【LeetCode-面试算法经典-Java实现】【所有题目目录索引】原题  Give...
  • DERRANTCM
  • DERRANTCM
  • 2015-08-08 06:35
  • 2580

java实现二叉树的构建以及3种遍历方法

转自:http://ocaicai.iteye.com/blog/1047397 目录:  1.把一个数组的值赋值给一颗二叉树  2.具体代码  1.树的构建方法  ...
  • xiepeifeng
  • xiepeifeng
  • 2015-01-31 14:39
  • 3154

二叉树的各种遍历,二叉树改链表,二叉树复习

二叉树的一个节点的结构如下: struct BinaryTree { int m_value; BinaryTree* left; BinaryTree* right; }; 二叉树的递归遍历 ...
  • zhouguoqionghai
  • zhouguoqionghai
  • 2015-08-31 19:29
  • 2231

PAT - 甲级 - 1020. Tree Traversals (25)(层次遍历)

题目描述: Suppose that all the keys in a binary tree are distinct positive integers. Given the postorde...
  • qq_34594236
  • qq_34594236
  • 2017-03-30 09:59
  • 261

pat甲级_树的遍历(例题:1020. Tree Traversals 、1086. Tree Traversals、1099. Build A Binary Search Tree)

一般pat考察树的题目会给出一个数组,数组中的数字将是构造树的元素。根据题目的一些特殊要求,可以构造出这棵树,然后根据题目的输出要求遍历输出这棵树。
  • ivanyang_tt
  • ivanyang_tt
  • 2016-07-27 16:19
  • 209

PAT 1020. Tree Traversals (25)(树的构造与遍历,通过后序中序输出层次遍历)

题目1020. Tree Traversals (25)时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN,...
  • qq_16949707
  • qq_16949707
  • 2016-09-06 13:17
  • 128

PAT甲级真题及训练集(19)--1020. Tree Traversals (25)(后序,中序建树,很重要)

1020. Tree Traversals (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 ...
  • u011662320
  • u011662320
  • 2017-07-04 16:58
  • 102

PAT甲级练习题A1020. Tree Traversals (25)

题目描述Suppose that all the keys in a binary tree are distinct positive integers. Given the postorder a...
  • muketangzhu
  • muketangzhu
  • 2016-08-14 20:30
  • 113

PAT甲级.1020. Tree Traversals (25)

题目 Suppose that all the keys in a binary tree are distinct positive integers. Given the postorder an...
  • R11happy
  • R11happy
  • 2016-10-23 10:48
  • 284

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

1020. Tree Traversals (25) Suppose that all the keys in a binary tree are distinct positive int...
  • liuchuo
  • liuchuo
  • 2016-08-06 18:31
  • 365
    个人资料
    • 访问:120853次
    • 积分:4558
    • 等级:
    • 排名:第7499名
    • 原创:349篇
    • 转载:0篇
    • 译文:0篇
    • 评论:7条
    最新评论