![](/Images/OutliningIndicators/ExpandedBlockStart.gif) /**//********************************************************************
purpose: 在二元树中找出和为某一值的所有路径
题目:输入一个整数和一棵二元树。
从树的根结点开始往下访问一直到叶结点所经过的所有结点形成一条路径。
打印出和与输入整数相等的所有路径。
例如 输入整数22和如下二元树
10
/ \
5 12
/ \
4 7
则打印出两条路径:10, 12和10, 5, 7。
![](/Images/OutliningIndicators/InBlock.gif)
二元树节点的数据结构定义为:
struct BinaryTreeNode // a node in the binary tree
{
int m_nValue; // value of node
BinaryTreeNode *m_pLeft; // left child of node
BinaryTreeNode *m_pRight; // right child of node
};
![](/Images/OutliningIndicators/InBlock.gif)
*********************************************************************/
#include <iostream>
#include <vector>
![](/Images/OutliningIndicators/None.gif)
using namespace std;
![](/Images/OutliningIndicators/None.gif)
struct BinaryTreeNode // a node in the binary tree
![](/Images/OutliningIndicators/ExpandedBlockStart.gif) ![](/Images/OutliningIndicators/ContractedBlock.gif) {
int m_nValue; // value of node
BinaryTreeNode *m_pLeft; // left child of node
BinaryTreeNode *m_pRight; // right child of node
};
![](/Images/OutliningIndicators/None.gif)
void PathEqualN(vector<BinaryTreeNode*> vect, BinaryTreeNode* pNode, int sum, int destSum)
![](/Images/OutliningIndicators/ExpandedBlockStart.gif) ![](/Images/OutliningIndicators/ContractedBlock.gif) {
![](/Images/OutliningIndicators/InBlock.gif)
sum += pNode->m_nValue;
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) if (pNode->m_pLeft == NULL && pNode->m_pRight == NULL) {
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) if (sum == destSum) {
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) for (int i = 0; i < vect.size(); i++) {
cout<<vect[i]->m_nValue<<" ";
}
cout<<pNode->m_nValue<<endl;
}
return;
}
![](/Images/OutliningIndicators/InBlock.gif)
vect.push_back(pNode);
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) if (pNode->m_pLeft != NULL) {
PathEqualN(vect, pNode->m_pLeft, sum, destSum);
}
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) if (pNode->m_pRight != NULL) {
PathEqualN(vect, pNode->m_pRight, sum, destSum);
}
vect.pop_back();
}
![](/Images/OutliningIndicators/None.gif)
void Test_PathEqualN()
![](/Images/OutliningIndicators/ExpandedBlockStart.gif) ![](/Images/OutliningIndicators/ContractedBlock.gif) {
// init tree
BinaryTreeNode node4; node4.m_nValue = 4; node4.m_pLeft = NULL; node4.m_pRight = NULL;
BinaryTreeNode node7; node7.m_nValue = 7; node7.m_pLeft = NULL; node7.m_pRight = NULL;
BinaryTreeNode node5; node5.m_nValue = 5; node5.m_pLeft = &node4; node5.m_pRight = &node7;
![](/Images/OutliningIndicators/InBlock.gif)
BinaryTreeNode node12; node12.m_nValue = 12; node12.m_pLeft = NULL; node12.m_pRight = NULL;
![](/Images/OutliningIndicators/InBlock.gif)
BinaryTreeNode root; root.m_nValue = 10; root.m_pLeft = &node5; root.m_pRight = &node12;
![](/Images/OutliningIndicators/InBlock.gif)
// search
vector<BinaryTreeNode*> vect;
PathEqualN(vect, &root, 0, 22);
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
}貌似都不是太难,难道是要考虑细节??
|
|
|
| 日 | 一 | 二 | 三 | 四 | 五 | 六 |
---|
24 | 25 | 26 | 27 | 28 | 29 | 30 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 1 | 2 | 3 | 4 |
|
导航
统计
- 随笔: 115
- 文章: 1
- 评论: 86
- 引用: 0
常用链接
留言簿(5)
随笔档案(115)
网址
搜索
积分与排名
最新评论
![](/images/xml.gif)
阅读排行榜
评论排行榜
|
|