码迷,mamicode.com
首页 > 其他好文 > 详细

HDU 1710 Binary Tree Traversals(二叉树)

时间:2020-04-25 10:36:06      阅读:49      评论:0      收藏:0      [点我收藏+]

标签:中序遍历   strong   ble   other   idt   des   col   后序   init   

Binary Tree Traversals

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

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.
技术图片
 

 

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
9 1 2 4 7 3 5 8 9 6 4 7 2 1 8 5 9 3 6
 
Sample Output
7 4 2 8 9 5 6 3 1

题目大意与分析

给出二叉树的先序遍历与中序遍历,求后序遍历

因为对于先序遍历来说,最先输出的是根节点,而对于中序遍历来说,根节点的左边都是左子树,根据这一特点,可以写出递归调用

#include<bits/stdc++.h>
using namespace std;
int a[1005],b[1005];
struct node
{
    int val;
    node *l,*r;
};

node *root;

node * pre_and_in_to_post(int *a,int *b,int n)  //a控制当前子树的先序遍历,b为中序,n为当前子树的长度 
{
    node *now;
    for(int i=0;i<n;i++)
    {
        if(a[0]==b[i])                //找到当前的根节点 
        {
            now=(node *)malloc(sizeof(node));
            now->val=b[i];
            now->l=pre_and_in_to_post(a+1,b,i);   //中序遍历的根节点左边的都在左子树上 
            now->r=pre_and_in_to_post(a+i+1,b+i+1,n-i-1);
            return now; 
        }
    }
    return NULL;
}
void postorder(node *t)
{
    if(t!=NULL)
    {
        postorder(t->l);
        postorder(t->r);
        if(t==root)
        cout<<t->val<<endl;
        else
        cout<<t->val<< ;
    }
}

int main()
{
    int n;
    while(cin>>n)
    {
        root=NULL;
        for(int i=0;i<n;i++)
        {
            cin>>a[i];
        }
        for(int i=0;i<n;i++)
        {
            cin>>b[i];
        }
        root=pre_and_in_to_post(a,b,n); 
        node *h=root;
        postorder(h);
    }
}

 

HDU 1710 Binary Tree Traversals(二叉树)

标签:中序遍历   strong   ble   other   idt   des   col   后序   init   

原文地址:https://www.cnblogs.com/dyhaohaoxuexi/p/12771676.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!