PathInTree
来源:互联网 发布:网络电视如何连接wifi 编辑:程序博客网 时间:2024/05/21 14:10
#define Nil 0#include<stdio.h>#include<stdlib.h>#include<vector>typedef struct BiTNode{int data;BiTNode* lchild;BiTNode* rchild;}*BiTree;void CreateBiTree(BiTree &T){int temp;printf("请输入节点的值!\n");scanf("%d",&temp);if(temp==Nil)T=NULL;else{T=(BiTNode*)malloc(sizeof(BiTNode));if(!T){printf("内存分配失败\n");return;}T->data=temp;CreateBiTree(T->lchild);CreateBiTree(T->rchild);}}void FindSumPath(const BiTree &T,int expectedSum,std::vector<int> &vector,int currentSum){currentSum+=T->data;vector.push_back(T->data);bool flag=T->lchild==NULL&&T->rchild==NULL;if(currentSum==expectedSum&&flag){std::vector<int>::iterator iter=vector.begin();while(iter!=vector.end()){printf("%3d",*iter);iter++;}printf("\n");}if(T->lchild)FindSumPath(T->lchild,expectedSum,vector,currentSum);if(T->rchild)FindSumPath(T->rchild,expectedSum,vector,currentSum);vector.pop_back();}void FindInPath(BiTree T,int expectedSum){if(T==NULL){printf("树为空\n");return;}int currentSum=0;std::vector<int> vector;FindSumPath(T,expectedSum,vector,currentSum);}void main(){int sum;BiTree T;CreateBiTree(T);printf("请输入节点的和\n");scanf("%d",&sum);FindInPath(T,sum);}
0 0
- PathInTree
- mac 安装 nginx 环境
- fail-safe cleanup (collections)问题出现
- Linux Shell 程序设计
- java中的集合框架LinkedList 简单用法
- 常见的几种网络请求框架比较
- PathInTree
- window平台如何搭建Mysql集群
- 一个操作系统的实现--内存管理中32位和64位的问题
- Opencv环境配置一遍就够了!!(.props避免身体被掏空,适用于其他需要配置各种目录的项目)
- 基于Java的毕业设计管理系统
- |Vijos|动态规划|P1057 盖房子
- PGM文件读写
- CSS3动画 @keyframes
- HDOJ 3790 最短路径问题(双权值最短路)