TreeNode *buildTree2(vector<int> &inorder, int istart, int iend, vector<int> &postorder, int pstart, int pend) { /* inorder的istart到iend和posterorder的pstart到pend,两个数组为中序遍历和后序遍历 4个int为下标 */ assert(iend - istart == pend - pstart);//个数相同 if (pend < pstart || iend < istart) { return NULL; } if (pend == pstart)//只有一个元素 { return new TreeNode(postorder[pend]); } int count; for (count = istart; count <= iend; count += 1) { if (inorder[count] == postorder[pend]) break; } count -= istart; TreeNode *root = new TreeNode(postorder[pend]); root->left = buildTree2(inorder, istart, istart + count - 1, postorder, pstart, pstart + count - 1); root->right = buildTree2(inorder, istart + count + 1, iend, postorder, pstart + count, pend - 1); return root; }
int main(int argc, const char *argv[]) { // the original tree struct node *root = createTree(); inOrderT(root); printf("\n"); preOrderT(root); printf("\n"); // creating inOrder and preOrder arrays int inOrderElements[10] = {15, 28, 32, 56, 60, 70, 75, 81, 83, 94}; int preOrderElements[10] = {60, 32, 15, 28, 56, 70, 83, 75, 81, 94}; struct node *inOrder[10]; struct node *preOrder[10]; int i = 0; for (i = 0; i < 10; i++) { inOrder[i] = newNode(inOrderElements[i]); } for (i = 0; i < 10; i++) { preOrder[i] = newNode(preOrderElements[i]); } int currRootIdx = 0; struct node *root2 = buildTree2(inOrder, preOrder, &currRootIdx, 0, 10 - 1); inOrderT(root2); printf("\n"); preOrderT(root2); printf("\n"); return 0; }
TreeNode *buildTree2(TreeNode ** in, TreeNode ** po, int count) { /* Given preorder and inorder traversal of a tree, construct the binary tree. Note: You may assume that duplicates do not exist in the tree. */ if (in == NULL || po == NULL)//参数有误 { return NULL; } if (count == 0) { return NULL; } if (count == 1) { assert(*in); assert(*in == *po); (*in)->right = NULL; (*in)->left = NULL; return *in; } int lcount = 0; for (lcount = 0; lcount < count; lcount += 1) { if (po[count - 1] == in[lcount]) { break; } } //左子树的节点数:lcount //右子树的节点数:count-lcount-1 (po[count - 1])->left = buildTree2(in, po, lcount); (po[count - 1])->right = buildTree2(in + lcount + 1, po + lcount, count - lcount - 1); return po[count - 1]; }
struct node *buildTree2(struct node *inOrder[], struct node *preOrder[], int *currRootIdx, int f, int l) { if (f < 0 || l >= 10 || f > l) { return NULL; } // Split the inOrder into two calls at root; recurse into two subtrees and // create the tree rooted at root int rootIdx; for (rootIdx = f; rootIdx <= l; rootIdx++) { if (inOrder[rootIdx]->data == preOrder[*currRootIdx]->data) { break; } } /* printf("(%d, %d) : %d, %d\n", f, l, rootIdx, *currRootIdx); */ // seek to the next root in the preOrder array (*currRootIdx)++; struct node *r = inOrder[rootIdx]; r->left = buildTree2(inOrder, preOrder, currRootIdx, f, rootIdx - 1); r->right = buildTree2(inOrder, preOrder, currRootIdx, rootIdx + 1, l); return r; }
TreeNode *buildTree2(vector<int> &inorder, vector<int> &postorder) { /* Given inorder and postorder traversal of a tree, construct the binary tree. Note: You may assume that duplicates do not exist in the tree. */ assert(inorder.size() == postorder.size()); if (inorder.size() == 0) { return NULL; } return buildTree2(inorder, 0, inorder.size() - 1, postorder, 0, postorder.size() - 1); }