算法题目---二叉树中和为某一值的路径
来源:互联网 发布:电信云计算 编辑:程序博客网 时间:2024/05/16 06:25
输入一棵二叉树和一个整数,打印出二叉树中结点值的各为输入整数的所有路径。从树的根结点开始往下一直到叶结点所经过的结点形成一条路径。
struct BinaryTreeNode
{int m_nValue;
BinaryTreeNode *m_pLeft;
BinaryTreeNode *m_pRight;
};
void FindPath(BinaryTreeNode *pRoot,int expectedSum,vector<int>&path,int ¤tSum)
{
currentSum += pRoot->m_nValue;
path.push_back(pRoot->m_nValue);
bool isLeaf = pRoot->m_pLeft == NULL && pRoot->m_pRight == NULL;
if(currentSum == expectedSum && isLeaf)
{
printf("A path is found: ");
vector<int>::iterator iter = path.begin();
for(;iter != path.end();++iter )
printf("%d\t ",*iter);
printf("\n");
}
if(pRoot->m_pLeft != NULL)
FindPath(pRoot->m_pLeft,expectedSum,path,currentSum);
if(pRoot->m_pRight != NULL)
FindPath(pRoot->m_pRight,expectedSum,path,currentSum);
currentSum -= pRoot->m_nValue;
path.pop_back();
}
void Find(BinaryTreeNode* pRoot,int expectedSum)
{
if(pRoot == NULL)
return;
vector<int> path;
int cur = 0;
FindPath(pRoot,expectedSum,path,cur);
}
void Test(char* testName, BinaryTreeNode* pRoot, int expectedSum)
{
if(testName != NULL)
printf("%s begins:\n", testName);
Find(pRoot, expectedSum);
printf("\n");
}
BinaryTreeNode* CreateBinaryTreeNode(int value)
{
BinaryTreeNode* pNode = new BinaryTreeNode();
pNode->m_nValue = value;
pNode->m_pLeft = NULL;
pNode->m_pRight = NULL;
return pNode;
}
void ConnectTreeNodes(BinaryTreeNode* pParent, BinaryTreeNode* pLeft, BinaryTreeNode* pRight)
{
if(pParent != NULL)
{
pParent->m_pLeft = pLeft;
pParent->m_pRight = pRight;
}
}
void DestroyTree(BinaryTreeNode* pRoot)
{
if(pRoot != NULL)
{
BinaryTreeNode* pLeft = pRoot->m_pLeft;
BinaryTreeNode* pRight = pRoot->m_pRight;
delete pRoot;
pRoot = NULL;
DestroyTree(pLeft);
DestroyTree(pRight);
}
}
void Test1()
{
BinaryTreeNode* pNode10 = CreateBinaryTreeNode(10);
BinaryTreeNode* pNode5 = CreateBinaryTreeNode(5);
BinaryTreeNode* pNode12 = CreateBinaryTreeNode(12);
BinaryTreeNode* pNode4 = CreateBinaryTreeNode(4);
BinaryTreeNode* pNode7 = CreateBinaryTreeNode(7);
ConnectTreeNodes(pNode10, pNode5, pNode12);
ConnectTreeNodes(pNode5, pNode4, pNode7);
printf("Two paths should be found in Test1.\n");
Test("Test1", pNode10, 22);
DestroyTree(pNode10);
}
void Test2()
{
BinaryTreeNode* pNode10 = CreateBinaryTreeNode(10);
BinaryTreeNode* pNode5 = CreateBinaryTreeNode(5);
BinaryTreeNode* pNode12 = CreateBinaryTreeNode(12);
BinaryTreeNode* pNode4 = CreateBinaryTreeNode(4);
BinaryTreeNode* pNode7 = CreateBinaryTreeNode(7);
ConnectTreeNodes(pNode10, pNode5, pNode12);
ConnectTreeNodes(pNode5, pNode4, pNode7);
printf("No paths should be found in Test2.\n");
Test("Test2", pNode10, 15);
DestroyTree(pNode10);
}
void Test3()
{
BinaryTreeNode* pNode5 = CreateBinaryTreeNode(5);
BinaryTreeNode* pNode4 = CreateBinaryTreeNode(4);
BinaryTreeNode* pNode3 = CreateBinaryTreeNode(3);
BinaryTreeNode* pNode2 = CreateBinaryTreeNode(2);
BinaryTreeNode* pNode1 = CreateBinaryTreeNode(1);
ConnectTreeNodes(pNode5, pNode4, NULL);
ConnectTreeNodes(pNode4, pNode3, NULL);
ConnectTreeNodes(pNode3, pNode2, NULL);
ConnectTreeNodes(pNode2, pNode1, NULL);
printf("One path should be found in Test3.\n");
Test("Test3", pNode5, 15);
DestroyTree(pNode5);
}
void Test4()
{
BinaryTreeNode* pNode1 = CreateBinaryTreeNode(1);
BinaryTreeNode* pNode2 = CreateBinaryTreeNode(2);
BinaryTreeNode* pNode3 = CreateBinaryTreeNode(3);
BinaryTreeNode* pNode4 = CreateBinaryTreeNode(4);
BinaryTreeNode* pNode5 = CreateBinaryTreeNode(5);
ConnectTreeNodes(pNode1, NULL, pNode2);
ConnectTreeNodes(pNode2, NULL, pNode3);
ConnectTreeNodes(pNode3, NULL, pNode4);
ConnectTreeNodes(pNode4, NULL, pNode5);
printf("No paths should be found in Test4.\n");
Test("Test4", pNode1, 16);
DestroyTree(pNode1);
}
void Test5()
{
BinaryTreeNode* pNode1 = CreateBinaryTreeNode(1);
printf("One path should be found in Test5.\n");
Test("Test5", pNode1, 1);
DestroyTree(pNode1);
}
void Test6()
{
printf("No paths should be found in Test6.\n");
Test("Test6", NULL, 0);
}
int main()
{
Test1();
Test2();
Test3();
Test4();
Test5();
Test6();
return 0;
}
阅读全文
0 0
- 算法题目---二叉树中和为某一值的路径
- 题目11:二叉树中和为某一值的路径
- 题目1368:二叉树中和为某一值的路径
- 题目1368:二叉树中和为某一值的路径
- 题目1368:二叉树中和为某一值的路径
- 题目1368:二叉树中和为某一值的路径
- 题目1368:二叉树中和为某一值的路径
- 二叉树中和为某一值的路径(算法)
- 算法复习:二叉树中和为某一值的路径
- 题目1368:二叉树中和为某一值的路径-九度
- 九度 题目1368:二叉树中和为某一值的路径
- 九度 题目1368:二叉树中和为某一值的路径
- 九度OJ-题目1368:二叉树中和为某一值的路径
- 剑指Offer题目1368:二叉树中和为某一值的路径
- 【树】二叉树中和为某一值的路径
- 【树6】二叉树中和为某一值的路径
- 二叉树中和为某一值的路径
- 二叉树中和为某一值的路径
- C++三大特性之封装
- python 数据库首字母大写 mysql 不区分大小写
- [插件/工具]微信小程序实用组件:带字母滑动的listview
- Android项目
- 匿名内部类简单实例
- 算法题目---二叉树中和为某一值的路径
- N-gram的原理、用途和研究
- python利用windows编译的sqlcipher工具对sqlite数据库加密
- 顶点缓冲对象(VBO)的用法
- POJ-3370 Halloween treats (抽屉原理)
- 广东省产业园区——清远华侨工业园
- java和mysql计算两个时间的天数
- js笔记八:class
- LeetCode 136. Single Number