重建二叉树-问题
1.QVector 代码-迭代器 {牛客网编不过 QT可以过}
TreeNode * reConstructBinaryTre2(QVector<int>::iterator startpre, QVector<int>::iterator endpre, QVector<int>::iterator startvin, QVector<int>::iterator endvin) { TreeNode *root; root = new TreeNode(*startpre);//初始化 if (startpre>endpre || startvin>endvin)//异常 return NULL; QVector<int>::iterator it; //在中序遍历中寻找根节点位置 for(it=startvin;it<=endvin;it++) { if(*it==*startpre) { int offset=it- startvin; root->left = reConstructBinaryTre2(startpre + 1, startpre + offset, startvin, it-1); root->right = reConstructBinaryTre2(startpre+offset + 1, endpre, it+1, endvin); break; } } return root; } TreeNode* reConstructBinaryTree(QVector<int> pre, QVector<int> vin) { //做检查 if(pre.empty()||vin.empty()) { return NULL; } int size=pre.size(); QVector<int>::iterator itpre=pre.begin(); //每一个迭代器对应一个容器 //和指针操作差不多 但不是指针 QVector<int>::iterator itvin=vin.begin(); TreeNode *root=reConstructBinaryTre2(itpre,itvin,itpre+size-1,itvin+size-1); return root; } int main(int argc, char *argv[]) { QCoreApplication a(argc, argv); QVector<int> pre1{ 1,2,4,7,3,5,6,8 }; QVector<int> in1{ 4,7,2,1,5,3,8,6 }; // pre1.resize(100); // in1.resize(100); // cout << pre1.size() << endl; // cout << in1.size() << endl; TreeNode * node; node = reConstructBinaryTree(pre1, in1); cout << node->val << endl; return a.exec(); }
2.只用QVector {QT编不过 牛客可以过}
TreeNode* reConstructBinaryTree1(QVector<int> pre, int startpre, int endpre, QVector<int> vin, int startin, int endin)//主要在 { TreeNode *root; root = new TreeNode(pre[startpre]);//初始化 if (startpre>endpre || startin>endin)//异常 return NULL; for (int i = startin;i <= endin;i++)// 在中序遍历 中找根节点 { if (vin[i] == pre[startpre]) { root->left = reConstructBinaryTree1(pre, startpre + 1, startpre + i - startin, vin, startin, i - 1); root->right = reConstructBinaryTree1(pre, (i - startin) + startpre + 1, endpre, vin, i + 1, endin); break; } } return root; } TreeNode* reConstructBinaryTree(QVector<int> pre, QVector<int> vin) { //做检查 if(pre.empty()||vin.empty()) { return NULL; } TreeNode * root = reConstructBinaryTree1(pre, 0, pre.size() - 1, vin, 0, vin.size() - 1); return root; }
3.李佳伟
/** * Definition for binary tree * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */ #include <iostream> using namespace std; #include<vector> struct TreeNode { int val; TreeNode *left; TreeNode *right; TreeNode(int x) : val(x), left(NULL), right(NULL) {} }; class Solution { public: TreeNode* construvt(vector<int>::iterator it1, vector<int>::iterator it2, vector<int>::iterator it3, vector<int>::iterator it4) { //if (!it1 || !it2 || !it3 || !it4) //迭代器是类,不是指针 //{ // return nullptr; //} int rootvalue = it1[0]; //获取根节点的值 //创建一颗二叉树 TreeNode* pptree = new TreeNode(rootvalue); //构造函数初始化数据域 pptree->val = rootvalue; //可有可无 pptree->left = nullptr; pptree->right = nullptr; vector<int>::iterator itt = it3; if (it1 == it2&&it3 == it4) { return pptree; } //在中序遍历中寻找根节点的位置 while (itt <= it4&&itt[0] != rootvalue) itt++; //计算左子树和右子树所含节点的个数 int leftnodenum = itt - it1; int rightnodenum = it4 - itt; //递归创建左子树 if (leftnodenum>0) { pptree->left = construvt(it1 + 1, it1 + leftnodenum, it3, it3 + leftnodenum); } //递归创建右子树 if (rightnodenum>0) { pptree->right = construvt(itt + 1 + leftnodenum, it2, itt + 1, it4); } return pptree; } public: TreeNode* reConstructBinaryTree(vector<int> pre, vector<int> vin) { if(pre.empty() || vin.empty()) { return nullptr; } int len = pre.size(); vector<int>::iterator itpre = pre.begin(); vector<int>::iterator itvin = vin.begin(); return construvt(itpre, itpre + len - 1, itvin, itvin + len - 1); } };