1020 Tree Traversals (25)(25 point(s))

时间:2022-08-26 22:23:13

problem

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

tip

给出后序与中序遍历,要求层次遍历。

answer

#include<iostream>
#include<queue>
#define Max 33 using namespace std; int n, back[Max], mid[Max];
//int tree[Max];
struct Node{
int d;
Node *l;
Node *r;
};
Node *root; void Print(){
queue<Node*> q;
q.push(root);
while(!q.empty()){
Node *t = q.front(); q.pop();
if(!t->d) continue;
if(!t->l && !t->r && q.empty()) cout<<t->d;
else cout<<t->d<<" "; if(t->l) q.push(t->l);
if(t->r) q.push(t->r);
}
// cout<<endl;
} Node* DFS(int left, int right, int midLeft, int midRight){
if(left > right) return NULL; Node *a = new Node();
int num = back[right];
int numIndex = -1;
for(int i = midLeft; i <= midRight; i++){
if(mid[i] == num) numIndex = i;
}
int lNum = numIndex - midLeft, rNum = midRight - numIndex;
a->d = num;
Print();
a->l = DFS(left, left + lNum -1, numIndex - lNum, numIndex-1);
a->r = DFS(right-rNum, right-1, numIndex+1, numIndex+rNum);
return a;
} int main(){
// freopen("test.txt", "r", stdin);
ios::sync_with_stdio(false); cin>>n;
for(int i = 0; i < n; i++){
cin>>back[i];
}
for(int i = 0; i < n; i++){
cin>>mid[i];
}
root = new Node();
root = DFS(0, n-1, 0, n-1);
Print();
return 0;
}

exprience

  • 二叉树要多写几次,就熟悉了。

1020 Tree Traversals (25)(25 point(s))的更多相关文章

  1. 03-树3 Tree Traversals Again(25 point&lpar;s&rpar;) 【Tree】

    03-树3 Tree Traversals Again(25 point(s)) An inorder binary tree traversal can be implemented in a no ...

  2. 03-树3 Tree Traversals Again(25 分)

    题目 链接 分析 push是二叉树前序遍历的结果,pop是二叉树中序遍历的结果,所以这个题就是已知前序遍历和中序遍历,求后序遍历. AC代码 #include "bits/stdc++.h& ...

  3. 03-树3 Tree Traversals Again (25 分&rpar;

    An inorder binary tree traversal can be implemented in a non-recursive way with a stack. For example ...

  4. 03-树3 Tree Traversals Again (25 分)

    An inorder binary tree traversal can be implemented in a non-recursive way with a stack. For example ...

  5. 1086 Tree Traversals Again (25 分)(二叉树的遍历)

    用栈来模拟一棵二叉树的先序遍历和中序遍历过程,求这棵二叉树的后序遍历 由题棵知道:push是先序遍历 pop是中序遍历 #include<bits/stdc++.h> using name ...

  6. 【PAT】1020 Tree Traversals &lpar;25&rpar;(25 分)

    1020 Tree Traversals (25)(25 分) Suppose that all the keys in a binary tree are distinct positive int ...

  7. PAT 甲级 1020 Tree Traversals &lpar;25分&rpar;(后序中序链表建树,求层序)&ast;&ast;&ast;重点复习

    1020 Tree Traversals (25分)   Suppose that all the keys in a binary tree are distinct positive intege ...

  8. PAT 甲级 1020 Tree Traversals &lpar;25 分&rpar;(二叉树已知后序和中序建树求层序)

    1020 Tree Traversals (25 分)   Suppose that all the keys in a binary tree are distinct positive integ ...

  9. PAT Advanced 1020 Tree Traversals &lpar;25 分&rpar;

    1020 Tree Traversals (25 分)   Suppose that all the keys in a binary tree are distinct positive integ ...

  10. PAT 1020 Tree Traversals&lbrack;二叉树遍历&rsqb;

    1020 Tree Traversals (25)(25 分) Suppose that all the keys in a binary tree are distinct positive int ...

随机推荐

  1. 七个结构模式之装饰者模式&lpar;Decorator Pattern&rpar;

    定义: 使用组合的方法,动态给一个类增加一些额外的功能,避免因为使用子类继承而导致类继承结构复杂.并且可以保持和被装饰者同一个抽象接口,从而使客户端透明. 结构图: Component:抽象构件类,定 ...

  2. iframe自动适应高度1

    js: function iFrameHeight() { var ifm= document.getElementById("iframepage"); var subWeb = ...

  3. 【Leetcode】【Medium】Set Matrix Zeroes

    Given a m x n matrix, if an element is 0, set its entire row and column to 0. Do it in place. 解题思路: ...

  4. RAC集群时间同步服务

    集群时间同步服务在集群中的两个 Oracle RAC 节点上执行以下集群时间同步服务配置.Oracle Clusterware 11g 第 2 版及更高版本要求在部署了 Oracle RAC 的集群的 ...

  5. 使用padding后内容超出父级元素

    解决方法:

  6. excel生成数据

    Sub function1()Dim i As LongFor i = 1 To 1000000Cells(i, 1) = "A" & iCells(i, 2) = &qu ...

  7. python的random&lpar;&rpar;函数

    python 的random函数需要调用 #!/usr/bin/python # -*- coding: UTF-8 -*- import random print( random.randint(1 ...

  8. MVC&vert; NuGet安装相关工具包

    ----------------------------------------------Ninject----------------------------------------------- ...

  9. numpy笔记—np&period;squeeze用法

    import numpy as np x = np.array([[[0], [1], [2]]]) print(x.shape) d = np.squeeze(x) # 从数组的形状中删除单维条目, ...

  10. delphi中TQueue的使用问题

    TQueue里存放的是指针,所要存储的内容最好建立在堆上,在pop方法之后释放掉这个空间. 实例代码: MMSQueue:= TQueue.Create; MMSQueue.Push(StrNew(P ...