[JOBDU] Topic 1521: Two image of a tree

Source: Internet
Author: User

I don't know what's going on. The following code passes 4 test cases, and 1 test cases are always runtime Error, and you can help me see where the problem is.

Image output Two methods, one is recursive to adjust, the other is directly on the basis of sequential traversal, the following code is implemented in the second

1#include <cstdio>2#include <cstdlib>3 4typedefstructbtnode{5     intkey;6     structBtnode *Lchild;7     structBtnode *Rchild;8 }btnode;9 TenBtnode *createbinarytree (intA[],intN) { OneBtnode *Nodes[n]; A      for(inti =0; I < n; ++i) { -Nodes[i] = (Btnode *)malloc(sizeof(Btnode)); -Nodes[i]->key =A[i]; theNodes[i]->lchild =NULL; -Nodes[i]->rchild =NULL; -     } -  +      for(inti =0; I < n; ++i) { -         Charstr[Ten]; +scanf"%s", str); A         if(str[0] =='D') { at             intLeft , right; -scanf"%d%d", &left, &Right ); -Nodes[i]->lchild = Nodes[left-1]; -Nodes[i]->rchild = Nodes[right-1]; -}Else if(str[0] =='L') { -             intLeft ; inscanf"%d", &Left ); -Nodes[i]->lchild = Nodes[left-1]; to}Else if(str[0] =='R') { +             intRight ; -scanf"%d", &Right ); theNodes[i]->rchild = Nodes[right-1]; *         } $     }Panax Notoginseng  -     returnnodes[0]; the } +  A /* the void Gettreemirror (Btnode *root) { + if (!root) - return; $ if (!root->lchild &&!root->rchild) $ return; -  - Btnode *temp = root->lchild; the root->lchild = root->rchild; - root->rchild = temp;Wuyi  the Gettreemirror (root->lchild); - Gettreemirror (root->rchild); Wu }*/ -  About voidPrinttreemirror (Btnode *root,intcount) { $     if(root) { -Count = =0? printf"%d", Root->key): printf ("%d", root->key); -Printtreemirror (Root->lchild, Count +1); -Printtreemirror (Root->rchild, Count +1); A     } + } the  - intMain () { $     intN; the      while(SCANF ("%d", &n)! =EOF) { the         intA[n]; the          for(inti =0; I < n; i++) thescanf"%d", &a[i]); -  inBtnode *root =Createbinarytree (A, n); thePrinttreemirror (Root,0); theprintf"\ n"); About     } the  the     return 0; the } + /************************************************************** - problem:1521 the User:tonyhuBayi language:c++ the result:runtime Error the ****************************************************************/

[JOBDU] Topic 1521: Two image of a 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.