Sdut 3343 Data structure Experiment two forks tree four: Restore binary tree

Source: Internet
Author: User

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*&AMP;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-&GT;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

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.