作者teybend (圈圈)
看板C_and_CPP
标题Re: [问题] 用结构指标建立binary tree
时间Mon Oct 19 17:40:03 2015
※ 引述《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://webptt.com/cn.aspx?n=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
1F:→ teybend: = = " 原来是最後面 给定错误 10/19 17:53
2F:→ tobygameac: 没细看code 不过最後几行那个root没new就使用了 10/19 23:03