中后序--->前

void makeTree() { cur = postorder.size() - 1; head = new node(postorder[cur]); int pos = inorder.find(postorder[cur]); cur--; bulidRight(pos + 1, inorder.size(), head); bulidLeft(0, pos, head); } void bulidLeft(int beg, int end, node* parent) { if(beg >= end || cur < 0) return; parent->leftChild = new node(postorder[cur]); int pos = inorder.find(postorder[cur]); cur--; bulidRight(pos + 1, end, parent->leftChild); bulidLeft(beg, pos, parent->leftChild); } void bulidRight(int beg, int end, node* parent) { if(beg >= end || cur < 0) return; parent->rightChild = new node(postorder[cur]); int pos = inorder.find(postorder[cur]); cur--; bulidRight(pos + 1, end, parent->rightChild); bulidLeft(beg, pos, parent->rightChild); }

你可能感兴趣的:(中后序--->前)