首页 > 其他分享 >1086 Tree Traversals Again

1086 Tree Traversals Again

时间:2023-05-20 11:11:48浏览次数:38  
标签:1086 遍历 end int Traversals Tree start Push root

题目:

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.


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

 

题目大意: 用栈的形式给出一棵二叉树的建立的顺序,求这棵二叉树的后序遍历

 

思路:根据给出的建立二叉树的栈操作,可以知道二叉树的前序遍历和中序遍历,从而就能够推导出二叉树的后序遍历。

如何得到前序遍历序列?每次Push操作的操作树按顺序插入数组,最终得到的数组就是前序遍历数组。

如何得到中序遍历序列?每次Pop操作从栈顶弹出的序号按顺序插入数组,最终得到的数组就是中序遍历数组。

 

由前序和中序遍历推到后序遍历的代码如下:(递归)

void postorder(int root, int start, int end) { // root是在前序遍历中根的下标 start 和 end是中序遍历的区间
    if (start > end) return;
    int i = start;
    while (i < end && inOrder[i] != preOrder[root]) i++;
    postorder(root + 1, start, i - 1); // left
    postorder(root + 1 + i - start, i + 1, end); //right
    postOrder[post++] = preOrder[root];
}

 

 

代码:

#include<stdio.h>
#include<iostream>
#include<string.h>
#include<stack>
using namespace std;
int preOrder[35], inOrder[35], postOrder[35];

int pre = 0, in = 0, post = 0;
stack<int> s;
int n;
void postorder(int root, int start, int end) {
    if (start > end) return;
    int i = start;
    while (i < end && inOrder[i] != preOrder[root]) i++;
    postorder(root + 1, start, i - 1);
    postorder(root + 1 + i - start, i + 1, end);
    postOrder[post++] = preOrder[root];
}
int main(){
    scanf("%d", &n);
    for(int i = 0; i < 2 * n; i++){
        string op; 
        int x;
        cin>>op;
        if(op == "Push"){
            cin>>x;
            s.push(x);
            preOrder[pre++] = x;
        }else if (op == "Pop"){
            inOrder[in++] = s.top();
            s.pop();
        }
    }
    postorder(0, 0, n - 1);
    for(int i = 0; i < post; i++){
        if(i != 0){
            cout<<" ";
        }
        cout<<postOrder[i];
    }
    return 0;
}

 

标签:1086,遍历,end,int,Traversals,Tree,start,Push,root
From: https://www.cnblogs.com/yccy/p/17416926.html

相关文章

  • 【做题记录】CodeForces343D Water Tree
    题面翻译给出一棵以\(1\)为根节点的\(n\)个节点的有根树。每个点有一个权值,初始为\(0\)。\(m\)次操作。操作有\(3\)种:将点\(u\)和其子树上的所有节点的权值改为\(1\)。将点\(u\)到\(1\)的路径上的所有节点的权值改为\(0\)。询问点\(u\)的权值。\(1\le......
  • el-tree实现自定义节点内容
    <!--*@Descripttion:el-tree实现自定义节点内容*@version:*@Author:zhangfan*@email:[email protected]*@Date:2020-07-0309:10:28*@LastEditors:zhangfan*@LastEditTime:2020-07-1611:21:20--><template><divclass="treeBo......
  • Uva--548 Tree(三个递归遍历/重建二叉树)
    记录23:132023-5-18uva.onlinejudge.org/external/5/548.htmlreference:《算法竞赛入门经典第二版》例题6-8使用中序遍历和后序遍历还原二叉树,还行,还是熟悉的。收获的点:使用数组快速建立二叉树(还是要变通,《数据结构与算法分析》中标准的使用了结构体指针,太过学术了?函数......
  • B. Fake Plastic Trees(贪心+dp)
    题目(FakePlasticTrees)[https://codeforces.com/problemset/problem/1693/B]题意输入T(≤1e3)表示T组数据。所有数据的n之和≤2e5。每组数据输入n(2≤n≤2e5)表示一棵n个节点的树,编号从1开始,1为根节点。然后输入p[2],p[3],...,p[n],其中p[i]表示i的父节......
  • 区块链实验-构建Merkle Tree
      主要内容:1.掌握MerkleTree的基本原理。2.编程实现MerkelTree的构建和数据完整性验证。实验条件:Win系统、Python实验内容:根据上图原理实现如下两个函数:#构建MerkleTreedefBuildTree(data):#验证数据完整性defValidate(hash,data):实现思......
  • Linux基础21 进程介绍, 进程监控状态ps, 进程相关命令pstree,pgrep,pidof, 动态进程监
    1.进程的管理:当我们运行一个程序,那么我们将该程序叫进程 进程线程协程 linux起服务会有给这个服务预分配的内存结构,windows没有 2.为什么要学进程管理?为了管理架构的服务 3.程序和进程的区别1)程序:开发写出来的代码,程序是永久存在的。 2)进程:它会随着程序的终止而销......
  • 1020 Tree Traversals
    题目:Supposethatallthekeysinabinarytreearedistinctpositiveintegers.Giventhepostorderandinordertraversalsequences,youaresupposedtooutputthelevelordertraversalsequenceofthecorrespondingbinarytree.InputSpecification:Eachi......
  • CF1777D Score of a Tree 题解
    题目简述给你一个\(n\)个结点根为\(1\)的树。在\(t=0\)时,每个结点都有一个值,为\(0\)或\(1\)。在每一个\(t>0\)时,每个结点的值都会变成其子结点在\(t-1\)时的值的异或和。定义\(S(t)\)为\(t\)时所有结点值的和。定义\(F(A)\)为树在\(0\let\le10^......
  • Java-Day-19( 对集合实现类的选择 + TreeSet + TreeMap )
    Java-Day-19总结-开发中如何选择集合实现类在开发中,选择什么集合实现类,主要取决于业务操作特点,然后根据集合实现类特性进行选择先判断存储的类型(一组对象或一组键值对)一组对象(单列):Collection接口允许重复:List增删多:LinkedList[底层维护了一个双向链......
  • AtCoder Beginner Contest 207 F Tree Patrolling
    洛谷传送门AtCoder传送门简单树形dp。设\(f_{u,i,p=0/1,q=0/1}\)为\(u\)的子树中被覆盖点数为\(i\),\(u\)有没有被覆盖,\(u\)有没有被选。转移树形背包合并即可,需要分类讨论。要注意如果\(u\)没被覆盖,\(v\)选了,或者\(u\)选了,\(v\)没被覆盖,被覆盖点数要\(+1\)。......