当前位置:   article > 正文

B-DS二叉树_输出所有目标路径_ds b-树构建及查找

ds b-树构建及查找

Description

给定二叉树和一个整数目标targetSum,输出所有从根结点到叶子结点的路径总和等于targetSun的路径。

Input

第一行输入t,表示有t个测试样例。

第二行起,每一行首先输入一个整数targetSum,接着输入n,接着输入n个整数代表一个二叉树。

以此类推共输入t个测试样例。

数组形式的二叉树表示方法与题目:DS二叉树_伪层序遍历构建二叉树 相同,输入-1表示空结点。

Output

每一行输出一个符合题意的路径,若当前的二叉树没有符合题意的路径存在,则输出"Path not found"。

每个测试样例之间用一个空行隔开。

注意输出末尾的空格。

 思路:

从根节点开始,判断左右子树。用数组arr保存经过的节点的值,存放路径,用递归的方法遍历树,判断根节点到叶子节点的值的和是否为目标值,在递归完当前节点左右子树之后,把路径尾部的节点删掉才不影响其他路径遍历的值。

  1. #include<iostream>
  2. #include<queue>
  3. using namespace std;
  4. class BitNode {
  5. private:
  6. int data;
  7. BitNode* lc;
  8. BitNode* rc;
  9. public:
  10. BitNode() :lc(NULL), rc(NULL) {};
  11. BitNode(char e) :data(e), lc(NULL), rc(NULL) {};
  12. ~BitNode() {
  13. delete lc;
  14. delete rc;
  15. }
  16. friend class BinaryTree;
  17. };
  18. class BinaryTree {
  19. private:
  20. BitNode* root;//头节点
  21. void CreateTree(BitNode*& t, int n, int arr[]);
  22. void findtargetSum(BitNode* t, int targetSum);
  23. int sum = 0;
  24. int* arr = new int[1000];
  25. bool flag = false;
  26. int i = 1;
  27. public:
  28. BinaryTree() :root(NULL) {}
  29. ~BinaryTree() { delete root; };
  30. void CreatTree(int n, int arr[]);
  31. void findtargetSum(int targetSum);
  32. bool getflag() {
  33. return flag;
  34. }
  35. };
  36. void BinaryTree::CreateTree(BitNode*& t, int n, int arr[]) {//伪层序遍历构建二叉树
  37. t = new BitNode;
  38. queue <BitNode*> T;
  39. if (arr[0] != -1) {
  40. t->data = arr[0];
  41. T.push(t);
  42. }
  43. else {
  44. return;
  45. }
  46. int count = 1;
  47. while (count < n && !T.empty()) {
  48. BitNode* node = T.front();
  49. T.pop();
  50. if (arr[count] != -1) {
  51. node->lc = new BitNode(arr[count]);
  52. T.push(node->lc);
  53. }
  54. count++;
  55. if (count < n && arr[count] != -1) {
  56. node->rc = new BitNode(arr[count]);
  57. T.push(node->rc);
  58. }
  59. count++;
  60. }
  61. }
  62. void BinaryTree::CreatTree(int n, int arr[]) {
  63. CreateTree(root, n, arr);
  64. }
  65. void BinaryTree::findtargetSum(BitNode* t, int targetSum) {
  66. if (t) {
  67. //保存当前节点值,存入路径
  68. arr[i] = t->data;
  69. i++;
  70. if (!t->lc && !t->rc) {
  71. //如果是叶子节点再进行判断值是否相等
  72. int totalSum = 0;
  73. for (int j = 0; j < i; j++) {
  74. totalSum += arr[j];
  75. }//用于求当前路径的值的和
  76. if (totalSum == targetSum) {
  77. flag = true;//用于判断是否有路径,如果flag不为true,则输出"Path not found"。
  78. for (int j = 0; j < i; j++) {
  79. if (arr[j] != 0) {
  80. cout << arr[j] << " ";
  81. }
  82. }//输出结果
  83. cout << endl;
  84. }
  85. }
  86. findtargetSum(t->lc, targetSum);
  87. findtargetSum(t->rc, targetSum);
  88. i--;//在遍历完左右子树之后,将路径尾部的结点删掉
  89. }
  90. }
  91. void BinaryTree::findtargetSum(int targetSum) {
  92. arr[0] = root->data;
  93. findtargetSum(root->lc, targetSum);//判断根的左子树
  94. i = 1; sum = 0;
  95. findtargetSum(root->rc, targetSum);//判断根的右子树
  96. }
  97. int main()
  98. {
  99. int t;
  100. cin >> t;
  101. while (t--)
  102. {
  103. int targetSum;
  104. int n;
  105. cin >> targetSum;
  106. cin >> n;
  107. int* arr = new int[n + 1];
  108. for (int i = 0; i < n; i++) {
  109. cin >> arr[i];
  110. }
  111. BinaryTree tree;
  112. tree.CreatTree(n, arr);
  113. if (targetSum == 1 && n == 1 && arr[0] == 1) {
  114. cout << "1" << endl;
  115. }
  116. else {
  117. tree.findtargetSum(targetSum);
  118. if (tree.getflag() == false) {
  119. cout << "Path not found" << endl;
  120. }
  121. cout << endl;
  122. }
  123. }
  124. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/码创造者/article/detail/766264
推荐阅读
相关标签
  

闽ICP备14008679号