hdu1710
意思就是给你一棵树,的前序遍历和中序遍历,求后序遍历。
分析
输入
9
1 2 4 7 3 5 8 9 6
4 7 2 1 8 5 9 3 6
前序中显示1是根节点,根据中序遍历的特点凡是中序在1左边的都在1节点的左子树,在1右边的都在1的右子树。1节点确定然后考虑递归构造左子树2,4,7,然后确定2,构造4,7…如果flag = -1,表示没有更新了可以结束。
//hud1710
#include <iostream>
#include <cstdio>
using namespace std;
const int N = 1010;
int pre[N], in[N], post[N];
int k;
struct node{
int val;
node * l, * r;
node(int val = 0, node * l = NULL, node * r = NULL): val(val), l(l), r(r){
}
};
inline void buildtree(int l, int</