博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
[转]求二叉树中和为给定值的所有路径
阅读量:6263 次
发布时间:2019-06-22

本文共 2800 字,大约阅读时间需要 9 分钟。

问题定义:

        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:关于本程序中建树部分,可以参考:)

 

代码实例:

#include 
#include
#include
#include
#include
#include
using namespace std; struct node { int data; struct node * lchild; struct node * rchild; }; //将数组转换为深度最低的二叉树,采用了二分查找的思想 struct node* ConvertArrayToTree(int data[], int first, int last) { if (last < first) { return NULL; } else { int mid = ( last + first ) / 2; struct node * newNode = NULL; newNode = (struct node *)malloc(sizeof(struct node)); newNode->data = data[mid]; newNode->lchild = ConvertArrayToTree(data, first, mid - 1); newNode->rchild = ConvertArrayToTree(data, mid + 1, last); return newNode; } } //再最左边插入一个节点 void InsertNodeAtLeft(struct node *root, struct node *newNode) { assert(root != NULL && newNode != NULL); while(root->lchild != NULL) { root = root->lchild; } root->lchild = newNode; } //在最右边插入一个节点 void InsertNodeAtRight(struct node *root, struct node *newNode) { assert(root != NULL && newNode != NULL); while(root->rchild != NULL) { root = root->rchild; } root->rchild = newNode; } //中序遍历 void Traverse(struct node *root) { if (root == NULL) { return; } Traverse(root->lchild); Traverse(root->rchild); printf("%d\t", root->data); } //打印和为sum的路径 void print(vector
& buffer, int first, int last) { int i; for (i = first; i <= last; i++) { cout << buffer[i] << "\t"; } cout << endl; } void findSum(struct node *head, int sum, vector
&buffer, int level) { if (head == NULL) return; int i; int tmp = sum; buffer.push_back(head->data); for (i = level; i >= 0; i--) { tmp -= buffer[i]; if (tmp == 0) print(buffer, i, level); } vector
lbuffer(buffer); vector
rbuffer(buffer); findSum(head->lchild, sum, lbuffer, level + 1); findSum(head->rchild, sum, rbuffer, level + 1); } int main(int argc, char* argv[]) { const int SIZE = 10;//测试的数据量 int data[SIZE];//保存数据 int i, j; struct node *head = NULL; for (i = 0; i < SIZE; i++) { data[i] = i + 1; } head = ConvertArrayToTree(data, 0, SIZE - 1); struct node *one = (struct node *)malloc(sizeof(struct node)); struct node *two = (struct node *)malloc(sizeof(struct node)); one->data = 11; one->lchild = NULL; one->rchild = NULL; two->data = 4; two->lchild = NULL; two->rchild = NULL; InsertNodeAtLeft(head, one); InsertNodeAtRight(head, two); //遍历数据 // Traverse(head); // printf("\n"); vector
v; findSum(head, 14, v, 0); return 0; }

该示例中所使用的二叉树如下所示:

运行结果如下:

转载地址:http://qwzpa.baihongyu.com/

你可能感兴趣的文章
面试真题-----hashMap原理
查看>>
js阻止事件冒泡 return false / e.stopPropagation() /e.preventDefault()
查看>>
CSS伪类使用
查看>>
哈佛成功金句
查看>>
iview Table表格单选框互斥
查看>>
leetcode278
查看>>
CodeForces-771D-Bear and Company
查看>>
PAT 1032 Sharing
查看>>
Extjs设置或获取cookie
查看>>
CC2541蓝牙BLE4.0主从透传工程
查看>>
iOS OC中block使用
查看>>
python之路--操作系统介绍,进程的创建
查看>>
markdown语法小结
查看>>
Java Gui 设计模式中的事件监听
查看>>
JavaSE-final关键字
查看>>
python自动化开发-1
查看>>
Remote远程特性的使用
查看>>
orm在django中的简单使用
查看>>
poj 2373 Dividing the Path
查看>>
dplyr 数据操作 常用函数(4)
查看>>