PAT L2 011 玩转树遍历 建树加BFS
通过两个已知遍历结果可以快速通过二分建二叉树,而层序遍历只要Bfs时把每个按顺序放入即可;
#include <stdio.h> #include <algorithm> #include <string.h> #include <queue> #include <iostream> using namespace std; typedef long long ll; const int N = 35; int cnt, n; typedef struct Tree { Tree *left; Tree *right; int val; }Tree; Tree *root; Tree* creat(int *preorder, int *inorder, int len) { Tree *tmp; for(int i = 0; i < len; i++) { if(preorder[0]==inorder[i]) { tmp = (Tree*)malloc(sizeof(Tree)); tmp->val = inorder[i]; tmp->left = creat(preorder+1, inorder, i); tmp->right = creat(preorder+i+1, inorder+i+1,len-(i+1)); return tmp; } } return NULL; } void levelorder(Tree *cur) { cnt = 0; queue<Tree*>que; while(!que.empty()) que.pop(); que.push(cur); Tree *node; while(!que.empty()) { node = que.front(); que.pop(); if(cnt == n-1) printf("%d\n", node->val); else { printf("%d ", node->val); cnt++; } if(node->right != NULL) que.push(node->right); if(node->left != NULL) que.push(node->left); } } int main() { // freopen("in.txt", "r", stdin); int preorder[N], inorder[N]; while(~scanf("%d", &n)) { for(int i = 0; i < n; i++) scanf("%d", &inorder[i]); for(int i = 0; i < n; i++) scanf("%d", &preorder[i]); root = creat(preorder, inorder, n); levelorder(root); } }