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

作者: teybend (圈圈)   2015-10-18 13:45:52
題目用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 findNextNode(int pstart,int istart,int size,string s)
{
if(size==0)
{
node leaf={NULL,NULL,' '};
return leaf;
}
node n={NULL,NULL,pre[pstart]};
if(size==1) {return n;}
int ipos=istart;
while(pre[pstart]!=in[ipos]){ipos++;}
int lsize=ipos - istart;
int rsize=size-lsize-1;
////////////////下兩行當return node 後會出現error//////////////
*n.left=findNextNode(pstart+1,istart,lsize,"left");
*n.right=findNextNode(pstart+lsize+1,ipos+1,rsize,"right");
}
void Posttrace(node p)
{
if(p.left!=NULL) Posttrace(*p.left);
if(p.right!=NULL) Posttrace(*p.right);
cout<<p.s<<" ";
}
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=findNextNode(0,0,strlen(pre),"parent");
Posttrace(root);
}
return 0;
}
http://plusaber.net/2015/08/26/Leetcode_Construct%20Binary%20Tree%20from%20Preorder%20and%20Inorder%20Traversal%20/
上面連結是我參考的網址 但我想把它改成結構方式
目前問題是編譯可以成功 但註解地方跑到時會出現error
跪求(1)解決方式可以跑出正確結果 跟(2)指標為何會有錯誤 觀點講解
願意解答(1) 願以500P幣 作為感謝 (2)100P幣 作為感謝
還請高手幫忙一下 !!

Links booklink

Contact Us: admin [ a t ] ucptt.com