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

首頁 > 學院 > 開發設計 > 正文

1020. Tree Traversals (25)

2019-11-11 06:56:26
字體:
來源:轉載
供稿:網友

2 3 1 5 7 6 4 1 2 3 4 5 6 7 得到root為4 左樹: 2 3 1 1 2 3 右樹: 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在中序某范圍內的位置{ 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);//構建樹木 bfstraverse(p);//層序遍歷樹 cout << endl;}
上一篇:Decision Tree

下一篇:LeetCode 67. Add Binary

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 锡林浩特市| 靖宇县| 绍兴市| 阿图什市| 乳源| 英吉沙县| 甘孜县| 佛冈县| 海林市| 弋阳县| 罗山县| 布尔津县| 永康市| 泗水县| 仁化县| 甘孜县| 昌邑市| 大田县| 凌源市| 吉水县| 阿巴嘎旗| 公安县| 延寿县| 裕民县| 潢川县| 永泰县| 赫章县| 阿城市| 根河市| 遂溪县| 平南县| 昌图县| 江山市| 怀来县| 肥城市| 蓝田县| 霍邱县| 色达县| 象州县| 海晏县| 拜泉县|