Data structure Experiment two forks tree four: Restore binary treeTime limit:1000ms Memory limit:65536kb Submit Statisticproblem Description
Given a binary tree's sequence of sequential traversal sequences and sequence traversal sequences, it is required to calculate the height of the two-fork tree.
Input data has multiple groups, the first row of each group of data entered1a positive integerN (1 <= n <=)is the total number of nodes in the tree, followed by2The first order and the middle sequence traversal sequence are given, all of which are lengthNdoes not contain duplicate English letters.(Case Sensitive)the string.
Output outputs an integer that is the height of the two fork tree. Example Input
9 ABDFGHIECFDHGIBEAC
Example Output
5
DQE:
This is titled Restoring a Binary Tree, given the sequence of the first order sequence and the middle sequence, in the restoration problem of the binary tree, it is known that the first order and the middle order or the known sequence and the middle order can restore the binary tree, the principle is the root node in order and order, thus the sequence of the Zuozi and right sub-tree is obtained, and the sequence of the middle sequence of Use pointer + reference recursive recovery, need to have a certain understanding of the pointer and then read this code.
1#include <iostream>2#include <cstdio>3 4 using namespacestd;5 6 structTree7 {8 CharC;9Tree *lt,*RT;Ten }; One ATree *creat (Char*&XX,Char*ZX) - { - if(*zx==' /') the returnNULL; - Char*x,*y; -Tree *r=NewTree; - intI=0; + while(zx[i]!=' /') - { + if(*xx==Zx[i]) A { atR->c=Zx[i]; -zx[i]=' /'; -x=ZX; -y=zx+i+1; -xx++; -r->lt=creat (xx,x); inr->rt=creat (xx,y); - Break; to } +i++; - } the returnR; * } $ Panax Notoginseng intDev (Tree *R) - { the if(r==NULL) + return 0; A intL=dev (R->LT), Rr=dev (r->RT); the intM=l>rr?L:RR; + returnm+1; - } $ $ intMain () - { - Charxx[ -],zx[ -],*p; theTree *Root; - intN;Wuyi while(SCANF ("%d", &n)! =EOF) the { -scanf"%s%s", XX,ZX); Wup=xx; -root=creat (P,ZX); Aboutprintf"%d\n", Dev (root)); $ } - return 0; - } - A /*************************************************** + User Name: * * * the result:accepted - Take time:0ms $ Take memory:160kb the Submit time:2016-11-03 19:06:10 the ****************************************************/
Sdut 3343 Data structure Experiment two forks tree four: Restore binary tree