最水的二叉树
#include#include using namespace std;int read(){ int s=0,f=1; char in=getchar(); while(in<'0'&&in>'9') { if(in=='-') f=-1; in=getchar(); } while(in>='0'&&in<='9') { s=(s<<1)+(s<<3)+in-'0'; in=getchar(); } return s*f;}int data[10000];struct node{ int value; int left; int right; node() { value=-0x7ffffff; left=-1; right=-1; }};node tree[1010];int tail;int build(int l,int r){ if(l>r) return -1; int num=++tail; int mid=(l+r)>>1; tree[num].value=data[mid]; tree[num].left=build(l,mid-1); tree[num].right=build(mid+1,r); return num;}void visit(int x){ if(x==-1) return ; visit(tree[x].left); printf("%d ",tree[x].value); visit(tree[x].right);}int insert(int val,int x){ if(x==-1) { tree[++tail].value=val; return tail; } if(val