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