在二元树中查找和为某一值的所有路径
2010-08-11 14:25
309 查看
#include <iostream>
#include <vector>
using namespace std;
struct BinaryTreeNode
{
int m_nValue;
BinaryTreeNode* m_pLeft;
BinaryTreeNode* m_pRight;
};
int count=0; //print control
void FindPath(
BinaryTreeNode* pTreeNode,
int expectedSum,
vector<int>& path,
int& currentSum)
{
if (!pTreeNode)
{
return;
}
currentSum+=pTreeNode->m_nValue;
path.push_back(pTreeNode->m_nValue);
bool isLeaf=!(pTreeNode->m_pLeft)&&!(pTreeNode->m_pRight);
if (currentSum==expectedSum && isLeaf)
{
vector<int>::iterator iter;
for (iter=path.begin(); iter!=path.end(); iter++)
{
cout<<*iter<<'\t';
}
cout<<endl;
}
if (pTreeNode->m_pLeft)
{
FindPath(pTreeNode->m_pLeft, expectedSum, path, currentSum);
}
if (pTreeNode->m_pRight)
{
FindPath(pTreeNode->m_pRight, expectedSum, path, currentSum);
}
//when finish visiting a node and return to its parent node,
//should delete this node from the path and
//minus the node's value from the current sum
currentSum-=pTreeNode->m_nValue;
path.pop_back();
}
void AddTree(BinaryTreeNode** T, int num)
{
if (*T==NULL)
{
*T=(BinaryTreeNode*)malloc(sizeof(BinaryTreeNode));
(*T)->m_nValue=num;
(*T)->m_pLeft=NULL;
(*T)->m_pRight=NULL;
}
else if ((*T)->m_nValue>num)
{
AddTree(&((*T)->m_pLeft), num);
}
else
{
AddTree(&((*T)->m_pRight), num);
}
}
void MidTraversal(BinaryTreeNode* T)
{
if(T!=NULL)
{
MidTraversal(T->m_pLeft);
count++;
if (count%5==0)
{
cout<<T->m_nValue<<endl;
}
else
{
cout<<T->m_nValue<<"\t";
}
MidTraversal(T->m_pRight);
}
}
int main()
{
BinaryTreeNode* T=NULL;
AddTree(&T, 10);
AddTree(&T, 12);
AddTree(&T, 5);
AddTree(&T, 7);
AddTree(&T, 4);
cout<<"Mid reversal tree is:\n";
MidTraversal(T);
vector<int> path;
int sum=0;
FindPath(T, 22, path, sum);
return 0;
}
#include <vector>
using namespace std;
struct BinaryTreeNode
{
int m_nValue;
BinaryTreeNode* m_pLeft;
BinaryTreeNode* m_pRight;
};
int count=0; //print control
void FindPath(
BinaryTreeNode* pTreeNode,
int expectedSum,
vector<int>& path,
int& currentSum)
{
if (!pTreeNode)
{
return;
}
currentSum+=pTreeNode->m_nValue;
path.push_back(pTreeNode->m_nValue);
bool isLeaf=!(pTreeNode->m_pLeft)&&!(pTreeNode->m_pRight);
if (currentSum==expectedSum && isLeaf)
{
vector<int>::iterator iter;
for (iter=path.begin(); iter!=path.end(); iter++)
{
cout<<*iter<<'\t';
}
cout<<endl;
}
if (pTreeNode->m_pLeft)
{
FindPath(pTreeNode->m_pLeft, expectedSum, path, currentSum);
}
if (pTreeNode->m_pRight)
{
FindPath(pTreeNode->m_pRight, expectedSum, path, currentSum);
}
//when finish visiting a node and return to its parent node,
//should delete this node from the path and
//minus the node's value from the current sum
currentSum-=pTreeNode->m_nValue;
path.pop_back();
}
void AddTree(BinaryTreeNode** T, int num)
{
if (*T==NULL)
{
*T=(BinaryTreeNode*)malloc(sizeof(BinaryTreeNode));
(*T)->m_nValue=num;
(*T)->m_pLeft=NULL;
(*T)->m_pRight=NULL;
}
else if ((*T)->m_nValue>num)
{
AddTree(&((*T)->m_pLeft), num);
}
else
{
AddTree(&((*T)->m_pRight), num);
}
}
void MidTraversal(BinaryTreeNode* T)
{
if(T!=NULL)
{
MidTraversal(T->m_pLeft);
count++;
if (count%5==0)
{
cout<<T->m_nValue<<endl;
}
else
{
cout<<T->m_nValue<<"\t";
}
MidTraversal(T->m_pRight);
}
}
int main()
{
BinaryTreeNode* T=NULL;
AddTree(&T, 10);
AddTree(&T, 12);
AddTree(&T, 5);
AddTree(&T, 7);
AddTree(&T, 4);
cout<<"Mid reversal tree is:\n";
MidTraversal(T);
vector<int> path;
int sum=0;
FindPath(T, 22, path, sum);
return 0;
}
相关文章推荐
- 在二元树中查找和为某一值的所有路径
- 数据结构面试题1.2.5-在二元树中找出和为某一值的所有路径
- 在二元树中找出和为某一值的所有路径-数据结构
- 每天三道冲刺工作--在二元树中找出和为某一值的所有路径
- 在二元树中找出和为某一值的所有路径
- 程序员面试题精选100题(04)-在二元树中找出和为某一值的所有路径
- 在二元树中找出和为某一值的所有路径
- 每天一道算法题(1)——在二元树中找出和为某一值的所有路径(java代码)
- 在二元树中找出和为某一值的所有路径
- 在二元树中找出和为某一值的所有路径
- 程序员面试100题---4.在二元树中找出和为某一值的所有路径(树)
- 第四题(在二元树中找出和为某一值的所有路径)
- 在二元树中找出和为某一值的所有路径
- 【面试题】在二元树中找出和为某一值的所有路径——Java实现
- 算法面试100题——4.在二元树中找出和为某一值的所有路径
- 微软等数据结构+算法面试100题(45)-- 在二元树中找出和为某一值的所有路径
- 百度一道面试题--在一颗二叉排序树中查找和为某一值的所有路径
- 在二元树中找出和为某一值的所有路径
- C++实现查找二叉树中和为某一值的所有路径的示例
- 在二元树中找出和为某一值的所有路径