nyoj756 Rebuilding a binary tree

Source: Internet
Author: User

Rebuilding the binary tree time limit: +Ms | Memory Limit:65535KB Difficulty:3
Describe
The topic is very simple, give you a binary tree after the order and the sequence, to find its pre-order sequence (so easy!).
Input
Input has multiple sets of data (less than 100 groups), ending with the end of the file.
Each group of data is only one row, including two strings, separated by a space, respectively, the order and sequence of the binary tree (string length is less than 26, input data to ensure legal).
Output
each set of output data takes a single row, and the output corresponds to a sequence of first order.
Sample input
Acbfged Abcdefgcdab Cbad
Sample output
Dbacegfbcad

Idea: To see the first thought of the subject is to reconstruct a binary tree, by the middle sequence traversal and post-order traversal can reconstruct a binary tree, and then the first sequence traversal output results

The code is as follows:

#include <stdio.h> #include <stdlib.h> #include <string.h>typedef struct node{char num;struct node * Lchild,*rchild;} Node,*pnode;void Buildtree (Pnode * root,char*post,char*in,int len)/*root represents the root node, post represents the result of the order traversal, in represents the result of the middle sequence traversal,     Len indicates the length of the current subtree */{if (len==0) {*root=null; return;} Pnode node= (pnode) malloc (sizeof (node)); Node->num=post[len-1];node->lchild=node->rchild=null;*root=node ; Char *s=strchr (in,node->num);//Find the location of the root node in the middle sequence traversal int leftlen=strlen (in)-strlen (s);//Zuozi length int rightlen= len-leftlen-1;//the length of the right subtree Buildtree (& (*root)->lchild,post,in,leftlen);//recursive construction left subtree Buildtree (& (*root) Rchild,post+leftlen,s+1,rightlen);//recursively establish right subtree}void bianlitree (pnode root)//First Order Traversal {if (root==null) return;p rintf ("%c",    Root->num); Bianlitree (Root->lchild); Bianlitree (root->rchild);}    int main () {char post[26],in[26]; while (scanf ("%s%s", post,in)!=eof) {Pnode root=null;buildtree (&root,post,in,strlen (in)); Bianlitree (root); printf ("\ n");} return 0;}

After the AC to see the Code of the Great God Instant tears Ben too simple to rebuild the binary tree, only need to get the results of each output can!

Great God Code:

#include <stdio.h> #include <string.h>void ReBuild (char *pre, char *in,char *post, int len) {if (len>0) {int N =STRCHR (in,post[len-1])-in; ReBuild (Pre+1,in,post,n); ReBuild (pre+n+1,in+n+1,post+n,len-n-1);p re[0]=post[len-1];}} int main () {char pre[30],in[30],post[30];int len;while (~scanf ("%s%s", Post,in)) {Len=strlen (POST); ReBuild (Pre,in,post,len);p re[len]=0;puts (pre);}}


nyoj756 Rebuilding a binary tree

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.