编程之美 3.9 重建二叉树
通过一棵树的先序和中序的遍历结果,重建这棵树。
解题思路:
还是递归,每一次在先序字符串中获得当前的根节点后,在中序字符串中找到它的位置,然后计算它的左子树和右子树对应的字符串开始与结束位置,并继续求它的左子树和右子树。重点是找清楚每次获得两个字符串的起始和结束位置!
结束条件是开始位置大于结束位置,返回NULL;
只有当节点的信息不重复时,才能正确地求得一棵的树。
#include<iostream> #include<string> #include<vector> #include<malloc.h> using namespace std; typedef struct NODE{ char data; struct NODE *lchild; struct NODE *rchild; }Node; void PrintNodeByLevel(const Node *root){ vector<const Node*> vec; vec.push_back(root); int cur = 0, last; int num =1; while(cur < vec.size()){ last = vec.size(); while(cur != last){ const Node *p = vec[cur]; cout << p->data; cur++; if (p->lchild){ vec.push_back(p->lchild); } if (p->rchild){ vec.push_back(p->rchild); } } cout << endl; } }
Node* RecurBTree(const string &first, const string &middle, int al, int ar, int bl, int br){ if (al > ar){ return NULL; } Node *root = (Node*)malloc(sizeof(root)); root->data = first[al]; int pos = middle.find_first_of(first[al]); root->lchild = RecurBTree(first, middle, al+1, al+pos-bl, bl, pos-1); root->rchild = RecurBTree(first, middle, al+pos-bl+1, ar, pos+1, br); return root; } int main(){ string s1 = "abdcef", s2 = "dbaecf"; cout << s2.find_first_of('a') << endl; Node *root = RecurBTree(s1, s2, 0, s1.length()-1, 0, s2.length()-1); PrintNodeByLevel(root); return 1; }