Chinaunix首页 | 论坛 | 博客
  • 博客访问: 519872
  • 博文数量: 184
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 1172
  • 用 户 组: 普通用户
  • 注册时间: 2016-06-21 13:40
个人简介

技术改变命运

文章分类

全部博文(184)

文章存档

2020年(16)

2017年(12)

2016年(156)

我的朋友

分类: C/C++

2016-07-05 17:42:47

2016.8.18改
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)。

点击(此处)折叠或打开

  1. #include<iostream>
  2. #include<assert.h>
  3. #include<vector>
  4. #include<malloc.h>
  5. using namespace std;
  6. struct treeNode
  7. {
  8.     struct treeNode *pLeft,*pRight;    
  9.     int p_val;
  10. };
  11. struct treeNode *createShortestTree(int *a,int lo,int hi)//构建高度最小的树  二分查找的方法
  12. {
  13.     if (lo > hi)//注意着里没有等号,这里不能省略,否则段错误
  14.         return NULL;
  15.     else
  16.     {
  17.         int mid = (lo+hi)/2;
  18.         int key = a[mid];
  19.         struct treeNode *pRoot = NULL;
  20.         pRoot = (struct treeNode *)malloc(sizeof(struct treeNode));
  21.         pRoot->p_val = key;
  22.         pRoot->pLeft = createShortestTree(a,lo,mid-1);
  23.         pRoot->pRight = createShortestTree(a,mid+1,hi);
  24.         return pRoot;
  25.     }
  26. }
  27. void insetLastLeft(struct treeNode *&pRoot,struct treeNode *pNew)
  28. {
  29.         assert(pRoot != NULL && pNew != NULL);
  30.         struct treeNode *root = pRoot;
  31.         while (root->pLeft)
  32.         {
  33.             root = root->pLeft;
  34.         }
  35.         root->pLeft = pNew;
  36. }
  37. void insertLastRight(struct treeNode *&pRoot,struct treeNode *pNew)
  38. {
  39.     assert(pRoot != NULL && pNew != NULL);//这里别忘了判断pNew
  40.     struct treeNode *root = pRoot;
  41.     while (root->pRight)
  42.     {
  43.         root = root->pRight;
  44.     }
  45.     root->pRight = pNew;
  46. }
  47. void print(vector<int> buffer,int first,int last)//顺序打印
  48. {
  49.     for (int i = first;i <= last;i++)
  50.     {
  51.         cout << buffer[i]<< "\t";
  52.     }
  53.     cout << "\n";
  54. }
  55. void findsum(struct treeNode *pRoot,int sum,vector<int> &buffer,int level)
  56. {
  57.     if (pRoot == NULL) return ;//这个终止条件别忘写了
  58.     int tmp = sum;
  59.     int i = 0;
  60.     buffer.push_back(pRoot->p_val);
  61.     for (i = level;i >= 0;i--)
  62.     {
  63.         tmp -= buffer[i];
  64.         if (tmp == 0) print(buffer,i,level);
  65.     }
  66.     vector<int> bufferl(buffer);//buffer1[0] = buffer[0],bufferl[1] = 新加入的节点的值....
  67.     vector<int> bufferr(buffer);
  68.     findsum(pRoot->pLeft,sum,bufferl,level+1);
  69.     findsum(pRoot->pRight,sum,bufferr,level+1);
  70. }
  71. int main()
  72. {
  73.     int i = 0;
  74.     int a[10];
  75.     vector<int> vec;
  76.     for (i = 0;i < 10;i++)
  77.     {
  78.         a[i] = i+1;
  79.     }
  80.     struct treeNode *pRoot = NULL;
  81.     pRoot = createShortestTree(a,0,9);
  82.     if (pRoot == NULL)
  83.     {
  84.         cout << "pRoot is null" << endl;
  85.     }
  86.     struct treeNode *new1 = (struct treeNode *)malloc(sizeof(struct treeNode));
  87.     new1->p_val = 11;
  88.     new1->pLeft = NULL;
  89.     new1->pRight = NULL;
  90.     struct treeNode *new2 = (struct treeNode *)malloc(sizeof(struct treeNode));
  91.     new2->p_val = 4;
  92.     new2->pLeft = NULL;
  93.     new2->pRight = NULL;
  94.     insetLastLeft(pRoot,new1);
  95.     insertLastRight(pRoot,new2);
  96.     findsum(pRoot,14,vec,0);
  97.     return 0;
  98. }
运行结果:
[root@localhost c++]# ./a.out
2       1       11
5       2       3       4
8       6
10      4


阅读(698) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~