1020.Tree Traversals (25)…to be continued…
pat-al-1020
2017-02-20
NODE* root = new NODE;參考:《算法筆記:上機訓練實戰指南》機械工業出版社/** * pat-al-1020 * 2017-02-18 * Cpp version * Author: fengLian_s */#include<stdio.h>#include<queue>#define MAX 50using namespace std;int post[MAX], in[MAX];struct NODE{ int data; NODE* lchild; NODE* rchild;};NODE* create(int postL, int postR, int inL, int inR){ if(postL > postR) return NULL; NODE* root = new NODE; root->data = post[postR]; int k; for(k = inL;k<= inR;k++) { if(in[k] == post[postR]) break; } int numLeft = k - inL;//左子樹的結點個數 root->lchild = create(postL, postL+numLeft-1, inL, k-1); root->rchild = create(postL+numLeft, postR-1, k+1, inR); return root;}void BFS(NODE* root){ int flagFirst = 1; queue<NODE*> q; q.push(root); while(!q.empty()) { NODE* tmp = q.front(); q.pop(); if(flagFirst == 1) { -TBC-新聞熱點
疑難解答