/*******************************************************************
100-4.题目:在二元树中找出和为某一值的所有路径
输入一个整数和一棵二元树。从树的根结点开始往下访问一直到叶结点所经过的所有结点形成一条路径。 打印出和与输入整数相等的所有路径。例如输入整数22 和二元树则打印出两条路径:10, 12 和10, 5, 7。
\*******************************************************************
#include#include using namespace std;typedef struct BTreeNode{ int data; BTreeNode *left; BTreeNode *right;}BTreeNode,*BTree;void add_btreenode(BTreeNode *&a_tree,int value){ if(a_tree == NULL){ a_tree = new BTreeNode(); a_tree->data = value; a_tree->left = NULL; a_tree->right = NULL; cout< data< data){ add_btreenode(a_tree->left,value); }else if(value > a_tree->data){ add_btreenode(a_tree->right,value); }}void preorder_btree(BTreeNode *a_tree,vector v,int sum,int given){ if(a_tree != NULL){ sum += a_tree->data; v.push_back(a_tree->data); cout<<"sum:"< <<':'< data<<' '; if(a_tree->right == NULL && a_tree->left == NULL){ if(sum == given){ for (vector ::iterator iter=v.begin();iter!=v.end();iter++){ cout<<*iter<<" "; } cout< left,v,sum,given); cout<<"r"; preorder_btree(a_tree->right,v,sum,given); v.pop_back(); sum -= a_tree->data; } }int main(){ int a[] ={4,3,5,2,9}; BTreeNode *a_tree=NULL; for(int i=0;i<5;i++){ cout< <<':'; add_btreenode(a_tree,a[i]); } cout<<"######"<