Http://acm.nyist.net/JudgeOnline/problem.php? PID = 1, 221

Source: Internet
Author: User

We know the first and middle order traversal of a tree, and find the back order traversal of the tree ,,,

For example:

Dbacepidermal abcdefg

Acbfged

ACCode:

 
# Include <stdio. h> # include <string. h> void build (int n, char * S1, char * S2) // construct the post-order traversal process {If (n <= 0) return; int P = strchr (S2, s1 [0])-S2; build (p, S1 + 1, S2); // access left subtree build (n-p-1, S1 + p + 1, s2 + p + 1); // access the right subtree printf ("% C", S1 [0]);} int main () {char a [27], B [27]; while (scanf ("% S % s", a, B) = 2) {int n = strlen (a); Build (n,, b); printf ("\ n");} return 0 ;}

Method 2:

# Include <iostream> # include <string> # include <algorithm> using namespace STD; typedef struct STR {char date; struct Str * l, * r;} * tire, T; tire build (string S, string S1) {tire u = NULL; If (S. size ()> 0) {u = new t; U-> date = s [0]; int K = s1.find (s [0]); u-> L = build (S. substr (1, k), s1.substr (0, k); U-> r = build (S. substr (k + 1), s1.substr (k + 1);} return U;} void delet (tire root) {If (root-> L) delet (root-> L); If (root-> r) delet (root-> r); Delete root;} void postorder (tire root) {If (root-> L) postorder (root-> L); If (root-> r) postorder (root-> r); cout <root-> date ;} int main () {string a, B; while (CIN> A> B) {tire root = build (a, B); postorder (Root ); cout <Endl; delet (Root);} return 0 ;}

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.