歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> 求二叉樹中和為給定值的所有路徑

求二叉樹中和為給定值的所有路徑

日期:2017/3/1 10:21:16   编辑:Linux編程


問題定義:

You are given a binary tree in which each node contains a value. Design an algorithm to print all paths which sum up to that value. Note that it can be any path in the tree-it does not have to start at the root.

解題思路:

一層一層的遍歷,保存當前節點到根節點的完整路徑,然後從當前節點向上掃描,如果找到了當前節點到某個節點的和等於給定值,則輸出之。程序對每個節點都需要遍歷一遍,還要掃描當前節點到根節點的路徑,且需要保存每個節點到根節點的路徑,所以時間復雜度為O(nlgn),空間復雜度為O(nlgn)。(ps:關於本程序中建樹部分,可以參考: http://www.linuxidc.com/Linux/2012-05/61459.htm )

代碼實例:

  1. #include <algorithm>
  2. #include <iostream>
  3. #include <time.h>
  4. #include <assert.h>
  5. #include <stdio.h>
  6. #include <vector>
  7. using namespace std;
  8. struct node
  9. {
  10. int data;
  11. struct node * lchild;
  12. struct node * rchild;
  13. };
  14. //將數組轉換為深度最低的二叉樹,采用了二分查找的思想
  15. struct node* ConvertArrayToTree(int data[], int first, int last)
  16. {
  17. if (last < first)
  18. {
  19. return NULL;
  20. }
  21. else
  22. {
  23. int mid = ( last + first ) / 2;
  24. struct node * newNode = NULL;
  25. newNode = (struct node *)malloc(sizeof(struct node));
  26. newNode->data = data[mid];
  27. newNode->lchild = ConvertArrayToTree(data, first, mid - 1);
  28. newNode->rchild = ConvertArrayToTree(data, mid + 1, last);
  29. return newNode;
  30. }
  31. }
  32. //再最左邊插入一個節點
  33. void InsertNodeAtLeft(struct node *root, struct node *newNode)
  34. {
  35. assert(root != NULL && newNode != NULL);
  36. while(root->lchild != NULL)
  37. {
  38. root = root->lchild;
  39. }
  40. root->lchild = newNode;
  41. }
  42. //在最右邊插入一個節點
  43. void InsertNodeAtRight(struct node *root, struct node *newNode)
  44. {
  45. assert(root != NULL && newNode != NULL);
  46. while(root->rchild != NULL)
  47. {
  48. root = root->rchild;
  49. }
  50. root->rchild = newNode;
  51. }
  52. //中序遍歷
  53. void Traverse(struct node *root)
  54. {
  55. if (root == NULL)
  56. {
  57. return;
  58. }
  59. Traverse(root->lchild);
  60. Traverse(root->rchild);
  61. printf("%d\t", root->data);
  62. }
  63. //打印和為sum的路徑
  64. void print(vector<int>& buffer, int first, int last)
  65. {
  66. int i;
  67. for (i = first; i <= last; i++)
  68. {
  69. cout << buffer[i] << "\t";
  70. }
  71. cout << endl;
  72. }
  73. void findSum(struct node *head, int sum, vector<int> &buffer, int level)
  74. {
  75. if (head == NULL) return;
  76. int i;
  77. int tmp = sum;
  78. buffer.push_back(head->data);
  79. for (i = level; i >= 0; i--)
  80. {
  81. tmp -= buffer[i];
  82. if (tmp == 0) print(buffer, i, level);
  83. }
  84. vector<int> lbuffer(buffer);
  85. vector<int> rbuffer(buffer);
  86. findSum(head->lchild, sum, lbuffer, level + 1);
  87. findSum(head->rchild, sum, rbuffer, level + 1);
  88. }
  89. int main(int argc, char* argv[])
  90. {
  91. const int SIZE = 10;//測試的數據量
  92. int data[SIZE];//保存數據
  93. int i, j;
  94. struct node *head = NULL;
  95. for (i = 0; i < SIZE; i++)
  96. {
  97. data[i] = i + 1;
  98. }
  99. head = ConvertArrayToTree(data, 0, SIZE - 1);
  100. struct node *one = (struct node *)malloc(sizeof(struct node));
  101. struct node *two = (struct node *)malloc(sizeof(struct node));
  102. one->data = 11;
  103. one->lchild = NULL;
  104. one->rchild = NULL;
  105. two->data = 4;
  106. two->lchild = NULL;
  107. two->rchild = NULL;
  108. InsertNodeAtLeft(head, one);
  109. InsertNodeAtRight(head, two);
  110. //遍歷數據
  111. // Traverse(head);
  112. // printf("\n");
  113. vector<int> v;
  114. findSum(head, 14, v, 0);
  115. return 0;
  116. }
該示例中所使用的二叉樹如下所示:


運行結果如下:

Copyright © Linux教程網 All Rights Reserved