重建二叉树
直接上代码了
struct BTNode { int val; BTNode *lchild; BTNode *rchild; BTNode(int x): val(x), lchild(NULL), rchild(NULL){} }; BTNode* rebuildBinTree(int a[], int b[], int n) { if (n <= 0) return NULL; BTNode *root = new BTNode(a[0]); int i; for (i = 0; i < n; ++i) { if (b[i] == a[0]) break; } root->lchild = rebuildBinTree(a+1, b, i); root->rchild = rebuildBinTree(a+i+1, b+i+1,n-i-1); return root; }