Tree UVA - 548 (DFS+建立二叉树)

男娘i 2022-02-28 13:22 302阅读 0赞

watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2FsZXgxOTk3MjIy_size_16_color_FFFFFF_t_70

解题思路:

根据中序后序建立二叉树,利用DFS便利每条路径的和

这里要注意输入:

  1. bool read_List(int* a){
  2. string line;
  3. if(!getline(cin,line)) return false; //如果为空,则直接退出
  4. stringstream ss(line); //化为stream流
  5. int x;
  6. n = 0;
  7. while(ss >> x) a[n++] = x;
  8. return n > 0;
  9. }

我们可以在读入字符串后,利用stringstream去除空格,把字符串中的数字全部读出来

  1. #include <iostream>
  2. #include <algorithm>
  3. #include <vector>
  4. #include <queue>
  5. #include <string>
  6. #include <sstream>
  7. using namespace std;
  8. const int MAXN = 10010;
  9. int midArr[MAXN],postArr[MAXN];
  10. int n;
  11. int leastNode = 0x3fffffff;
  12. int minRoute = 0x3fffffff;
  13. vector<int> routes;
  14. bool flag = true;
  15. bool read_List(int* a){
  16. string line;
  17. if(!getline(cin,line)) return false; //如果为空,则直接退出
  18. stringstream ss(line); //化为stream流
  19. int x;
  20. n = 0;
  21. while(ss >> x) a[n++] = x;
  22. return n > 0;
  23. }
  24. struct stNode{
  25. int stData;
  26. stNode *lchild,*rchild;
  27. stNode(int _value){
  28. stData = _value;
  29. lchild = rchild = nullptr;
  30. }
  31. };
  32. //DFSfind
  33. void DFSfind(stNode* sroot){
  34. if(sroot->lchild == nullptr && sroot->rchild == nullptr){
  35. routes.push_back(sroot->stData);
  36. //求和
  37. int tempSum = 0;
  38. for(int k = 0; k < routes.size();++k){
  39. tempSum += routes[k]; //加上tempSum的和
  40. }
  41. if(tempSum == minRoute){
  42. if(sroot->stData < leastNode){
  43. leastNode = sroot->stData;
  44. }
  45. }
  46. else if(tempSum < minRoute){
  47. minRoute = tempSum;
  48. leastNode = sroot->stData;
  49. }
  50. routes.pop_back();
  51. }
  52. if(sroot->lchild != nullptr){
  53. routes.push_back(sroot->stData);
  54. DFSfind(sroot->lchild);
  55. routes.pop_back(); //出队
  56. }
  57. if(sroot->rchild != nullptr){
  58. routes.push_back(sroot->stData);
  59. DFSfind(sroot->rchild);
  60. routes.pop_back();
  61. }
  62. }
  63. stNode* buildTree(int posL,int posR,int inL,int inR){
  64. if(posL > posR) return nullptr;
  65. int curposRoot = postArr[posR];
  66. int curInroot = -1;
  67. int k;
  68. for(int i = inL;i <= inR;++i){
  69. if(midArr[i] == curposRoot){
  70. curInroot = midArr[i];
  71. k = i;
  72. break;
  73. }
  74. }
  75. if(curInroot == -1) return nullptr;
  76. stNode* croot = new stNode(curposRoot);
  77. int numK = k - inL;
  78. croot->lchild = buildTree(posL,posL+numK-1,inL,k-1); //向左
  79. croot->rchild = buildTree(posL+numK,posR-1,k+1,inR); //向右
  80. return croot; //返回头结点
  81. }
  82. int main(){
  83. while(read_List(midArr)){
  84. read_List(postArr);
  85. leastNode = 0x3fffffff;
  86. minRoute = 0x3fffffff;
  87. routes.clear();
  88. stNode* troot = buildTree(0,n-1,0,n-1); //建树
  89. DFSfind(troot);
  90. cout<<leastNode<<endl;
  91. }
  92. system("PAUSE");
  93. return 0;
  94. }

发表评论

表情:
评论列表 (有 0 条评论,302人围观)

还没有评论,来说两句吧...

相关阅读

    相关 S-Trees UVA712(

    题目读了半天,差点被吓住!题目本身很简单,就是一颗满二叉树,向左(2\temp),向右(2\temp+1),最后减去(1<<n)-1;(即非叶子结点的个数,因为储存叶子结点是从

    相关 Tree UVA 548(DFS)

    解题思路:对于给定的二叉树的中序遍历和后序遍历,可以构造出这棵二叉树,方法是根据后序遍历找到树根。然后在中序遍历中找到树根,从而找出左右子树的结点列表,然后递归构造左