Solve the traversal problem: codevs and codevs

Source: Internet
Author: User

Solve the traversal problem: codevs and codevs

1029 Traversal Problems
1029 traversal ProblemsTime Limit: 1 s space limit: 128000 KB title level: Diamond Title Description Description

We are all very familiar with the pre-order, middle-order, and post-order traversal of Binary Trees. In the data structure, we often ask the following question: We know the pre-order and Middle-order traversal of a binary tree, calculate its post-order traversal. Accordingly, you can also find its pre-order traversal by knowing the post-order traversal of a binary tree and the middle-order traversal sequence. However, given the forward and backward orders of a binary tree, you cannot determine the sequential traversal sequence. Consider the following Binary Trees:

 

All these Binary Trees share the same pre-order traversal and post-order traversal, but the middle-order traversal is different.

Input description Input Description

There are two rows in the input file. The first row indicates the result of the previous traversal of the tree, and the second row indicates the result of the later traversal of the tree. The entered character set is {a-z}, and the length cannot exceed 26.

Output description Output Description

The output file contains only one integer that cannot exceed a long integer, indicating the total number of possible sequential traversal sequences.

Sample Input Sample Input

Abc

CBA

Sample output Sample Output

4

Ideas

Only one son's node can have different Middle-order traversal when he knows the pre-order and post-order, so he converts the question into finding the number of nodes with only one son.

It is easy to find out the rules that appear in the forward and backward orders of such nodes. (If AB appears in the first order and BA appears in the latter order, this node has only one son)

Each node of this type has two types of sequential traversal (with the son on the left and the son on the right). According to the multiplication principle, the number of sequential traversal is 2 ^ number of nodes.

Code
#include<cstdio>#include<cstring>#include<algorithm>using namespace std;char qian[30];char hou[30];int ans=0;int main(){    scanf("%s%s",qian,hou);    for(int i=0;i<=strlen(qian)-1;i++)        for(int j=1;j<=strlen(hou)-1;j++)            if(qian[i]==hou[j]&&qian[i+1]==hou[j-1])                ans++;    printf("%d",1<<ans);}
2010 Post-order traversal
2010 perform post-order traversal

 

Time Limit: 1 s space limit: 64000 KB title level: Silver Title Description Description

Enter the first and middle traversal sequences of a binary tree, and output the latter.

Input description Input Description

 

There are two rows in total. The first row is a string, indicating the first traversal of the tree. The second row is a string, indicating the middle traversal of the tree.

Output description Output Description

Only one row indicates the post-order traversal sequence of the tree.

Sample Input Sample Input

Abdehicfg

Dbheiafcg

Sample output Sample Output

Dhiebfgca

Data range and prompt Data Size & Hint

The input length is not greater than 255.

Code

#include<iostream>using namespace std;int len;string s1,s2;void dfs(int s,int l,int r){    if(l==r)return;    int pos=l;    for(int i=l;i<=r;i++)if(s1[s]==s2[i]){pos=i;break;}    dfs(s+1,l,pos);    dfs(s+pos-l+1,pos+1,r);    cout<<s1[s];}int main(){    cin>>s1>>s2;    len=s1.length();    dfs(0,0,len);}
3143 Sequential traversal of Binary Trees
3143 sequential traversal of Binary Trees

 

Time Limit: 1 s space limit: 32000 KB title level: Silver Title Description Description

Returns the forward traversal of a binary tree.

Input description Input Description

The first line is an integer n, indicating the number of nodes in the tree.

The next n rows have two integers (L and R) in each row. The two integers Li and Ri in line I represent the left Son Number and the right son number of the node numbered I.

Output description Output Description

The output consists of three rows, namely, forward traversal, central traversal, and backward traversal. Numbers are separated by spaces.

Sample Input Sample Input

5

2 3

4 5

0 0

0 0

0 0

Sample output Sample Output

1 2 4 5 3

4 2 5 1 3

4 5 2 3 1

Data range and prompt Data Size & Hint

N <= 16

Code

#include<iostream>using namespace std;int n;struct node{    int l,r;}tree[20];void dfs(int v){    cout<<v<<' ';    if(tree[v].l!=0)dfs(tree[v].l);    if(tree[v].r!=0)dfs(tree[v].r);}void afs(int v){    if(tree[v].l!=0)afs(tree[v].l);    cout<<v<<' ';    if(tree[v].r!=0)afs(tree[v].r);}void cfs(int v){    if(tree[v].l!=0)cfs(tree[v].l);    if(tree[v].r!=0)cfs(tree[v].r);    cout<<v<<' ';}int main(){    cin>>n;    int x,y;    for(int i=1;i<=n;i++)    {        cin>>x>>y;        tree[i].l=x;        tree[i].r=y;    }    dfs(1);cout<<endl;    afs(1);cout<<endl;    cfs(1);}

 

 

 

 

 

 

 

 

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.