Re: [問題] 用結構指標建立binary tree

看板C_and_CPP (C/C++)作者 (圈圈)時間10年前 (2015/10/19 17:40), 10年前編輯推噓0(002)
留言2則, 2人參與, 最新討論串1/1
※ 引述《teybend (圈圈)》之銘言: : ※ [本文轉錄自 Programming 看板 #1M8p8J5N ] : 作者: teybend (圈圈) 看板: Programming : 標題: [問題] 用結構指標建立binary tree : 時間: Sun Oct 18 13:45:52 2015 如果我把它改成回傳指標呢並且改掉一些遞迴錯誤?為什麼還是不行? : 題目用preorder & inorder 建立binary tree並用postorder輸出 : 我的code: : #include <iostream> : #include <algorithm> : #include<string.h> : using namespace std; : char pre[27]; : char in[27]; : struct node : { : node *left; : node *right; : char s; node(char c) :s(c),left(NULL),right(NULL) {} : }; : node *findNextNode(int pstart,int istart,int size,string s) : { if(size==0) { return NULL; } node *n=new node(pre[pstart]); : int ipos=istart; : while(pre[pstart]!=in[ipos]){ipos++;} : int lsize=ipos - istart; : int rsize=size-lsize-1; n->left=findNextNode(pstart+1,istart,lsize,"left"); n->right=findNextNode(pstart+lsize+1,ipos+1,rsize,"right"); return n; : } : int main() : { : int m; : //這邊m只是輸入要跑幾次 無關重要 : cin>>m; : for(int i=0;i<m;i++) : { : int n ; : cin>>n; : //輸入preorder inorder : for(int i=0;i<n;i++) { cin>>pre[i]; } : for(int i=0;i<n;i++) { cin>>in[i]; } node *root; root->left=findNextNode(0,0,strlen(pre),"parent"); : } : return 0; : } 編譯一樣可以通過 但還是無法成功 QQ -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 140.113.136.218 ※ 文章網址: https://www.ptt.cc/bbs/C_and_CPP/M.1445247604.A.F4A.html ※ 編輯: teybend (140.113.136.218), 10/19/2015 17:43:01 ※ 編輯: teybend (140.113.136.218), 10/19/2015 17:44:51 ※ 編輯: teybend (140.113.136.218), 10/19/2015 17:52:36

10/19 17:53, , 1F
= = " 原來是最後面 給定錯誤
10/19 17:53, 1F

10/19 23:03, , 2F
沒細看code 不過最後幾行那個root沒new就使用了
10/19 23:03, 2F
文章代碼(AID): #1M9BfqzA (C_and_CPP)
文章代碼(AID): #1M9BfqzA (C_and_CPP)