Hdoj topic 1710 binary tree traversals (binary search trees)

Source: Internet
Author: User

Binary Tree traversalsTime limit:1000/1000 MS (java/others) Memory limit:32768/32768 K (java/others)
Total submission (s): 3912 Accepted Submission (s): 1741


Problem Descriptiona binary tree is a finite set of vertices that is either empty or consists of a root r and a disjoint Binary trees called the left and right subtrees. There is three most important ways in which the vertices of a binary tree can be systematically traversed or ordered. They is preorder, inorder and Postorder. Let T is 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, the n the vertices of T2 in preorder.

In a inorder traversal of the vertices of T, we visit the vertices of T1 in inorder, then the root R, followed by the Ver Tices 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 postor Der and finally we visit R.

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

Inputthe input contains several test cases. The first line of all test case contains a single integer n (1<=n<=1000), the number of vertices of the binary tree . followed by lines, respectively indicating the preorder sequence and inorder sequence. You can assume they is 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

Sourcehdu 2007-spring Programming Contest
Recommendlcy | We have carefully selected several similar problems for you:1707 1512 1714 1175 1044 sequential and pre-order traversal of AC code
#include <stdio.h> #include <string.h> #include <stdlib.h>typedef struct s{struct s *l,*r;int num;} Node,*node; Node root;int N;int a[3000],b[3000]; Node creat (int *a,int *b,int N) {node tree;for (int i=0;i<n;i++) {if (A[0]==b[i]) {tree= (node) calloc (1,sizeof (node)); Tree->num=b[i];tree->l=creat (a+1,b,i); Tree->r=creat (a+i+1,b+i+1,n-i-1); return tree;}} return NULL;} void Houxu (Node cur) {//node cur=root;if (cur!=null) {Houxu (cur->l); Houxu (cur->r); if (cur==root) printf ("%d", Cur->num); elseprintf ("%d", Cur->num);}} int main () {//node root;root= (node) calloc (1,sizeof (node)), while (scanf ("%d", &n)!=eof) {int i;for (i=0;i<n;i++) {scanf ("%d", &a[i]);} for (i=0;i<n;i++) scanf ("%d", &b[i]), Root=creat (A,b,n), Houxu (root);p rintf ("\ n");} while (1);}


Hdoj topic 1710 binary tree traversals (binary search trees)

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.