国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 學(xué)院 > 開(kāi)發(fā)設(shè)計(jì) > 正文

1020. Tree Traversals (25)

2019-11-11 07:35:13
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

2 3 1 5 7 6 4 1 2 3 4 5 6 7 得到root為4 左樹(shù): 2 3 1 1 2 3 右樹(shù): 5 7 6 5 6 7 遞歸

#include<iostream>#define MAX_Node 32#include<deque>using namespace std;int post[MAX_Node];//保存后序int in[MAX_Node];//保存中序int N;typedef struct Node{ int data; struct Node *lchild; struct Node *rchild;}Node,*Tree;int findN(int x,int a)//尋找root在中序某范圍內(nèi)的位置{ for (int t = 0;t < N;t++) if (x == in[t+a]) return t; return 0;}Tree findchild(int m,int n,int x,int y){ if (m > n) {return NULL; } Node *root=(Node *)malloc(sizeof(Node)); root->data = post[n]; int mid = findN(root->data,x); root->lchild=findchild(m, m+mid-1,x,x+mid-1); root->rchild=findchild(m+mid, n-1, x+mid+1,y); return root;}void bfstraverse(Node *p){ deque<Node *> que; cout << p->data; if (p->lchild != NULL) que.push_back(p->lchild); if (p->rchild != NULL) que.push_back(p->rchild); while (!que.empty()) { if (que.front()->lchild != NULL) que.push_back(que.front()->lchild); if (que.front()->rchild != NULL) que.push_back(que.front()->rchild); cout << " "<<que.front()->data; que.pop_front(); }}int main(){ Node *p; cin >> N; for (int t = 0;t < N;t++) cin >> post[t]; for (int t = 0;t < N;t++) cin >> in[t]; p=findchild(0, N - 1,0,N-1);//構(gòu)建樹(shù)木 bfstraverse(p);//層序遍歷樹(shù) cout << endl;}
發(fā)表評(píng)論 共有條評(píng)論
用戶(hù)名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 景洪市| 本溪| 新和县| 兴国县| 遵义市| SHOW| 许昌县| 平罗县| 甘洛县| 武陟县| 衡东县| 滦南县| 长岭县| 同仁县| 南漳县| 平阴县| 大埔县| 七台河市| 新泰市| 大洼县| 广河县| 隆昌县| 金阳县| 平乡县| 凤台县| 于都县| 灯塔市| 南漳县| 南召县| 霍邱县| 长顺县| 京山县| 白山市| 阳曲县| 凤城市| 安岳县| 北宁市| 灵宝市| 沂南县| 雷州市| 保山市|