首页 > 其他分享 >Binary Tree Traversals

Binary Tree Traversals

时间:2023-03-25 13:34:21浏览次数:46  
标签:Binary preorder int Traversals Tree vertices binary inorder postorder


Binary Tree Traversals

Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 9623    Accepted Submission(s): 4351


Problem Description

A binary tree is a finite set of vertices that is either empty or consists of a root r and two disjoint binary trees called the left and right subtrees. There are three most important ways in which the vertices of a binary tree can be systematically traversed or ordered. They are preorder, inorder and postorder. Let T be a binary tree with root r and subtrees T1,T2.


In a preorder traversal of the vertices of T, we visit the root r followed by visiting the vertices of T1 in preorder, then the vertices of T2 in preorder.



In an inorder traversal of the vertices of T, we visit the vertices of T1 in inorder, then the root r, followed by the vertices of T2 in inorder.



In a postorder traversal of the vertices of T, we visit the vertices of T1 in postorder, then the vertices of T2 in postorder and finally we visit r.



Now you are given the preorder sequence and inorder sequence of a certain binary tree. Try to find out its postorder sequence.



Binary Tree Traversals_i++

 


Input

The input contains several test cases. The first line of each test case contains a single integer n (1<=n<=1000), the number of vertices of the binary tree. Followed by two lines, respectively indicating the preorder sequence and inorder sequence. You can assume they are always correspond to a exclusive binary tree.

 


Output

For each test case print a single line specifying the corresponding postorder sequence.

 


Sample Input


91 2 4 7 3 5 8 9 64 7 2 1 8 5 9 3 6

 

Sample Output


7 4 2 8 9 5 6 3 1

 


Source

HDU 2007-Spring Programming Contest

 


Recommend

lcy

思想:先序可以寻找根节点,中序可以找到左右子树

代码实现:

#include<bits/stdc++.h>
using namespace std;
int a[1005],b[1005];
void cal(int be,int en,int mid)//递归输出mid每次输出结点(可以看作根节点)
{
    if(en<be) return ;
    int i;
    for(i=be;i<=en;i++)
    {
        if(b[i]==a[mid])//在中序找到根节点
            break;
    }
    cal(be,i-1,mid+1);//此时i为根节点,左子树必定在begin到i-1里面。  mid+1是因为前序的左子树为mid+1,此时作为根,  
    cal(i+1,en,mid-be+i+1);//此时i为跟节点,右子树必定在i+1到end里面。 同理  
    cout<<a[mid];
    if(mid==1)
        cout<<endl;
    else
        cout<<" ";
}
int main()
{

    int n;
    while(cin>>n)
    {
    for(int i=1;i<=n;i++)
        scanf("%d",&a[i]);
    for(int i=1;i<=n;i++)
        scanf("%d",&b[i]);
     cal(1,n,1);
    }
    return 0;
}


标签:Binary,preorder,int,Traversals,Tree,vertices,binary,inorder,postorder
From: https://blog.51cto.com/u_14932227/6149362

相关文章

  • 【hdu6547】Tree(树链剖分, 线段树区间根号)
    problemalgorihtm1、树链剖分什么是树链剖分(重链剖分)?树链,就是树上的路径。剖分,就是把路径分类为重链和轻链。对于树上的一个点,与其相连的边中,连向的节点子树大小最大(重儿子)......
  • segment_tree
    这是一棵一棵一棵线段树众所周知,线段树是一个优质的维护区间信息(需要满足结合律)的数据结构我们可以\(O(nlogn)\)建树\(O(logn)\)查询任意区间的信息关于结合......
  • CF EC Round 145 D. Binary String Sorting
    D题意给一个01串,交换两个数需要花费\(10^{12}\),删除某个数需要花费\(10^{12}+1\),问最少花费多少使得串单调不降思路线性dp,\(f[i][0]\)表示前i位构建的串结尾为0,单调......
  • Segment Tree Beats! 初步和其他
    不会渐进表示,全无脑用\(\Theta\)了/kk目录区间最值问题不含区间加减的情况划分数域历史最值问题双半群模型问题描述使用标记维护的问题区间最值问题不含区间加减的......
  • Debunking Rumors on Twitter with Tree Transformer
    Article:l 论文标题:DebunkingRumorsonTwitterwithTreeTransformer(利用树状Transformer模型揭露Twitter中的谣言)l 论文作者:JingMa、WeiGaol 论文来源:2020......
  • Mysql B-Tree与B+Tree区别
    一、B-Tree与B+Tree介绍B-TreeB-Tree是一种平衡树,用于支持快速的查找、插入和删除操作。B-Tree通常被用作关系数据库管理系统(RDBMS)的索引结构,因为它能够在大数据集合中......
  • POJ - 3321 Apple Tree
    原题链接思路答案不好直接维护,所以,我们可以采用DFS序来解决这一问题。设\(l_u\)是以\(u\)为根的子树中最小的时间戳,\(r_u\)是以\(u\)为根的子树中最大的时间戳......
  • 题解 ABC294G【Distance Queries on a Tree】
    DFS序树状数组。不妨以\(1\)为根,设\(\operatorname{dep}(u)\)表示\(u\)到根路径的边权和,\(\operatorname{dis}(u,v)\)表示\(u,v\)间路径的边权和,\(\operatornam......
  • Treemap按key和value降序排序
    Treemap是一种根据键排序的数据结构,可以通过重载它的比较器来按照值排序。要按键排序,可以使用默认的比较器,而要按值排序,可以创建一个自定义的比较器并将其传递给treemap的......
  • Tree Master (根号分治,离散化)
    题目大意:给出一个树,每次给出2个相同高度的点,然后依次向父亲走,问val[a]*val[b]这些值加起来是多少 思路:直接map映射关联容器,时间复杂度过大根号分治?于是......