D-Two cross-tree traversal (recommended)

Source: Internet
Author: User

Binary Tree Traversal problem

Description

Tree Recovery

Little Valentine liked playing with binary trees very much. Her favorite game is constructing randomly looking binary trees with capital letters in the nodes.

This is a example of one of her creations:

                                    D                                   /                                   /                                    B     E                                /\                                    /   \     \                               A     C     G                                         /                                        /                                       F

To record hers trees for the future generations, she wrote down the strings for each tree:a preorder traversal (root, left Sub Tree, right subtree) and a inorder traversal (left subtree, root, right subtree).

For the tree drawn above the preorder traversal are DBACEGF and the inorder traversal is ABCDEFG.

She thought that such a pair of strings would give enough information to reconstruct the tree later (but she never tried I T).

Now, years later, looking again in the strings, she realized that reconstructing the trees is indeed possible, but only B Ecause she never had used the same letter twice in the same tree.

However, doing the reconstruction by hand, soon turned off to be tedious.

So now she asks you to write a program this does the job for her!

Input SpecificationThe input file is contain one or more test cases. Each test case consists of one line containing the strings Preord and Inord, representing the preorder traversal and Inord ER traversal of a binary tree. Both strings consist of unique capital letters.   (Thus they is not longer than characters.)

Input is terminated by end of file.

Output Specificationfor each test case, recover Valentine's binary tree and print one line containing the tree's Postorder traversal (left   Subtree, right subtree, root).

Sample Input
DBACEGF Abcdefgbcad Cbad

Sample Output
Acbfgedcdab

Main topic:

The first order and the middle order of the binary tree are known.

Analysis:

The two-fork tree is traversed by recursion.

Code:

1#include <iostream>2#include <cstring>3 using namespacestd; 4   5 Charpre[ -],mid[ -]; 6   7 voidTintP1,intP2,intQ1,intQ2,intK//recursion for Traversal8 {  9     if(p1>p2)Ten         return;  One      for(k=q1;mid[k]!=pre[p1];++k);  AT (p1+1, p1+k-q1,q1,k-1,0);  -T (p1+k-q1+1, p2,k+1, Q2,0);  -cout<<Mid[k];  the }   -    - intMain () - {   +      while(cin>>pre>>mid) -     {   +         intL=strlen (PRE)-1;  AT0L0L0);  atcout<<Endl;  -     }   -     return 0;  -}

Experience:
Binary tree problem looks very easy, but to the first order, the sequence, and the order of understanding, can better do the problem of binary tree. Binary tree problem is nothing more than two order to find a sequence, is generally used recursive method to achieve, to learn recursion, recursive application of many aspects.

D-Two cross-tree traversal (recommended)

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.