HDU 1710 Binary Tree traversals

Source: Internet
Author: User
Problem descriptiona 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.
 


Inputthe 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.
 


Outputfor 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


The first node of the first traversal must be the root node of the tree. Then, find the root node in the middle traversal, and all the elements on the left of the root node of the Middle-order array are in the left subtree, the elements on the right side are located in the right subtree until the leaf node.

#include <stdio.h>#include <string.h>#include <math.h>#include <iostream>#include <queue>#include <algorithm>#define mem(f) memset(f,0,sizeof(f))#define M 100005#define mod 1000000007#define lson o<<1, l, m#define rson o<<1|1, m+1, rusing namespace std;typedef long long LL;const int MAX = 0x3f3f3f3f;const int maxn = 1005;int n, k, pre[maxn], post[maxn], mid[maxn];void build(int l1, int r1, int l2, int r2) {    if(l1 > r1 || l2 > r2) return;    int o = pre[l1], pos;    for(int i = l2; ; i++) {        if(mid[i] == o) {            pos = i;            break;        }    }    int cntl = pos-l2, cntr = r2-pos; //printf("%d %d\n", cntl, cntr);    build(l1+1, l1+cntl, l2, pos-1);    build(l1+cntl+1, r1, pos+1, r2);    post[k++] = o;}int main(){    while(~scanf("%d", &n)) {        for(int i = 1; i <= n; i++) scanf("%d", &pre[i]);        for(int i = 1; i <= n; i++) scanf("%d", &mid[i]);        k = 0;        build(1, n, 1, n);        for(int i = 0; i < k-1; i++) printf("%d ",post[i]);        printf("%d\n", post[k-1]);    }    return 0;}



Zookeeper

HDU 1710 Binary Tree traversals

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.