//在二元树中找到和为某一值的所有路径
//从树的根节点开始往下访问一直到叶节点所经过的所有结点形成的一条路径。
//打印出荷与输入整数相等的所有路径
#include <iostream>
#include <vector>
using namespace std;
typedef int value_type;
class BinaryNode
{
public:
BinaryNode *left;
BinaryNode *right;
value_type value;
BinaryNode(int v)
{
left = NULL;
right = NULL;
value = v;
}
};
class BinaryTree
{
public:
BinaryTree();
void FindPath(BinaryNode *pNode, vector<int> path, int CurrentSum, int ExpectSum);
void CreateTree(BinaryNode * & pNode);
void InOrderTraverse(BinaryNode * pNode);
BinaryNode *root;
};
BinaryTree::BinaryTree()
{
root = NULL;
}
void BinaryTree::CreateTree(BinaryNode* & pNode)
{
int value;
scanf("%d", &value);
if (value == 0)
{
pNode = NULL;
}
else
{
pNode = new BinaryNode(value);
CreateTree(pNode->left);
CreateTree(pNode->right);
}
}
void BinaryTree::FindPath(BinaryNode *pNode, vector<int> path, int CurrentSum, int ExpectSum)
{
if(pNode == NULL)
{
return;
}
CurrentSum += pNode->value;
path.push_back(pNode->value);
if(pNode->left == NULL && pNode->right == NULL)
{
if(CurrentSum == ExpectSum)
{
for(vector<int>::iterator iter = path.begin(); iter != path.end(); iter ++)
{
cout << *iter <<" ";
}
cout << endl;
}
return;
}
FindPath(pNode->left, path, CurrentSum, ExpectSum);
FindPath(pNode->right, path, CurrentSum, ExpectSum);
}
void BinaryTree::InOrderTraverse(BinaryNode *pNode)
{
if (pNode == NULL)
{
return;
}
InOrderTraverse(pNode->left);
cout << pNode->value <<" ";
InOrderTraverse(pNode->right);
}
int main()
{
vector<int> path;
int CurrentSum;
int ExpectSum;
cout << "建立二叉树" << endl;
BinaryTree tree;
tree.CreateTree(tree.root);
tree.InOrderTraverse(tree.root);
cout << endl;
int num = 0;
while(scanf("%d", &ExpectSum)!=EOF)
{
CurrentSum = 0;
path.clear();
tree.FindPath(tree.root, path, CurrentSum, ExpectSum);
}
return 0;
}
/*
建立二叉树
10 5 2 0 0 7 0 0 15 10 0 0 20 0 0
2 5 7 10 10 15 20
17
10 5 2
22
10 5 7
35
10 15 10
45
10 15 20
*/
//从树的根节点开始往下访问一直到叶节点所经过的所有结点形成的一条路径。
//打印出荷与输入整数相等的所有路径
#include <iostream>
#include <vector>
using namespace std;
typedef int value_type;
class BinaryNode
{
public:
BinaryNode *left;
BinaryNode *right;
value_type value;
BinaryNode(int v)
{
left = NULL;
right = NULL;
value = v;
}
};
class BinaryTree
{
public:
BinaryTree();
void FindPath(BinaryNode *pNode, vector<int> path, int CurrentSum, int ExpectSum);
void CreateTree(BinaryNode * & pNode);
void InOrderTraverse(BinaryNode * pNode);
BinaryNode *root;
};
BinaryTree::BinaryTree()
{
root = NULL;
}
void BinaryTree::CreateTree(BinaryNode* & pNode)
{
int value;
scanf("%d", &value);
if (value == 0)
{
pNode = NULL;
}
else
{
pNode = new BinaryNode(value);
CreateTree(pNode->left);
CreateTree(pNode->right);
}
}
void BinaryTree::FindPath(BinaryNode *pNode, vector<int> path, int CurrentSum, int ExpectSum)
{
if(pNode == NULL)
{
return;
}
CurrentSum += pNode->value;
path.push_back(pNode->value);
if(pNode->left == NULL && pNode->right == NULL)
{
if(CurrentSum == ExpectSum)
{
for(vector<int>::iterator iter = path.begin(); iter != path.end(); iter ++)
{
cout << *iter <<" ";
}
cout << endl;
}
return;
}
FindPath(pNode->left, path, CurrentSum, ExpectSum);
FindPath(pNode->right, path, CurrentSum, ExpectSum);
}
void BinaryTree::InOrderTraverse(BinaryNode *pNode)
{
if (pNode == NULL)
{
return;
}
InOrderTraverse(pNode->left);
cout << pNode->value <<" ";
InOrderTraverse(pNode->right);
}
int main()
{
vector<int> path;
int CurrentSum;
int ExpectSum;
cout << "建立二叉树" << endl;
BinaryTree tree;
tree.CreateTree(tree.root);
tree.InOrderTraverse(tree.root);
cout << endl;
int num = 0;
while(scanf("%d", &ExpectSum)!=EOF)
{
CurrentSum = 0;
path.clear();
tree.FindPath(tree.root, path, CurrentSum, ExpectSum);
}
return 0;
}
/*
建立二叉树
10 5 2 0 0 7 0 0 15 10 0 0 20 0 0
2 5 7 10 10 15 20
17
10 5 2
22
10 5 7
35
10 15 10
45
10 15 20
*/