本文转自:http://zhedahht.blog.163.com/blog/static/254111742007228357325/
题目:输入一个整数和一棵二元树。从树的根结点开始往下访问一直到叶结点所经过的所有结点形成一条路径。打印出和与输入整数相等的所有路径。
例如输入整数22和如下二元树
10
/ \
5 12
/ \
4 7
则打印出两条路径:10, 12和10, 5, 7。
分析:这是百度的一道笔试题,考查对树这种基本数据结构以及递归函数的理解。
当访问到某一结点时,把该结点添加到路径上,并累加当前结点的值。如果当前结点为叶结点并且当前路径的和刚好等于输入的整数,则当前的路径符合要求,我们把它打印出来。如果当前结点不是叶结点,则继续访问它的子结点。当前结点访问结束后,递归函数将自动回到父结点。因此我们在函数退出之前要在路径上删除当前结点并减去当前结点的值,以确保返回父结点时路径刚好是根结点到父结点的路径。我们不难看出保存路径的数据结构实际上是一个栈结构,因为路径要与递归调用状态一致,而递归调用本质就是一个压栈和出栈的过程。
这道题是《剑指Offer——名企面试官精讲典型编程题》一书的第25题,书中讲的更详细。
下面是代码:
#include <iostream>
#include <vector>
#include <stdio.h>
using namespace std;
struct BinaryTreeNode
{
int m_nValue;
BinaryTreeNode* m_pLeft;
BinaryTreeNode* m_pRight;
};
//创建节点
BinaryTreeNode* CreateBinaryTreeNode(int value)
{
BinaryTreeNode* pNode = new BinaryTreeNode();
pNode->m_nValue = value;
pNode->m_pLeft = pNode->m_pRight = NULL;
return pNode;
}
//添加pLeft到pParent的左孩子
bool BinaryTreeAddLeftNode(BinaryTreeNode* pParent,BinaryTreeNode* pLeft)
{
if(NULL == pParent || NULL == pLeft)
return false;
if(pParent->m_pLeft != NULL)
{
printf("%d has left child\n",pParent->m_nValue);
return false;
}
else
{
pParent->m_pLeft = pLeft;
return true;
}
}
//添加pRight到pParent的右孩子
bool BinaryTreeAddRightNode(BinaryTreeNode* pParent,BinaryTreeNode* pRight)
{
if(NULL == pParent || NULL == pRight)
return false;
if(pParent->m_pRight != NULL)
{
printf("%d has right child\n",pParent->m_nValue);
return false;
}
else
{
pParent->m_pRight = pRight;
return true;
}
}
//完成寻找路径的核心函数,采用先序遍历树。
void BinaryTreeFindPathWithExpectSumCore(BinaryTreeNode* pNode,int expectSum,vector<int>& path,int& currentSum)
{
//将当前节点值加入路径
currentSum += pNode->m_nValue;
path.push_back(pNode->m_nValue);
bool isLeaf = (NULL == pNode->m_pLeft && NULL == pNode->m_pRight);
if(isLeaf && currentSum == expectSum)
{
printf("A path has found:\n");
vector<int>::iterator iter = path.begin();
for(iter; iter!=path.end(); iter++)
printf("%d ",*iter);
printf("\n");
}
if(pNode->m_pLeft != NULL)
BinaryTreeFindPathWithExpectSumCore(pNode->m_pLeft,expectSum,path,currentSum);
if(pNode->m_pRight != NULL)
BinaryTreeFindPathWithExpectSumCore(pNode->m_pRight,expectSum,path,currentSum);
//将当前节点值从路径中删除
currentSum -= pNode->m_nValue;
path.pop_back();
}
void BinaryTreeFindPathWithExpectSum(BinaryTreeNode* pRoot,int expectSum)
{
if(NULL == pRoot)
return;
int currentSum = 0;
vector<int> path;
BinaryTreeFindPathWithExpectSumCore(pRoot,expectSum,path,currentSum);
}
int main()
{
//构建树
BinaryTreeNode* pRoot = CreateBinaryTreeNode(10);
BinaryTreeNode* pNode5 = CreateBinaryTreeNode(5);
BinaryTreeNode* pNode12 = CreateBinaryTreeNode(12);
BinaryTreeNode* pNode4 = CreateBinaryTreeNode(4);
BinaryTreeNode* pNode7 = CreateBinaryTreeNode(7);
BinaryTreeAddLeftNode(pRoot,pNode5);
BinaryTreeAddRightNode(pRoot,pNode12);
BinaryTreeAddLeftNode(pNode5,pNode4);
BinaryTreeAddRightNode(pNode5,pNode7);
//测试程序
BinaryTreeFindPathWithExpectSum(pRoot,22);
}
上面程序的测试用例分如下几种:1.功能测试:二叉树中有一条、多条符合条件的路径,二叉树中没有符合条件的路径。
2.特殊输入测试:指向二叉树根节点的指针为NULL。
以上面构造的二叉树来说测试用例为:
//测试用例:
//路径中存在和为19,并且是一条路径
//BinaryTreeFindPathWithExpectSum(pRoot,19);
//路径中存在和为22,并且是多条路径
//BinaryTreeFindPathWithExpectSum(pRoot,22);
//路径中不存在和为27
//BinaryTreeFindPathWithExpectSum(pRoot,27);
//参数不合法
BinaryTreeFindPathWithExpectSum(NULL,-3);
在上面的代码中,我们用标准模板库中的vector实现了一个栈来保存路径,每一次都用push_back在路径的末尾添加节点,用pop_back在路径的末尾删除节点,这样就保证了栈的先入后出的特性。这里没有直接使用STL中的stack的原因是stack中只能得到栈顶元素,而我们打印路径的时候需要得到路径上所有节点,因此在代码实现的时候stack不是最好的选择。
上面的程序如果已知所有的节点都是正数的话,可以用剪枝法优化,sum值如果大于期望值,下面的路径就不用遍历了,否则,如果节点值可以为负的话就不能用剪枝法优化。
如果能保证所有节点值都为正的话,用剪枝法优化的代码如下:
//完成寻找路径的核心函数,采用先序遍历树。
void BinaryTreeFindPathWithExpectSumCore(BinaryTreeNode* pNode,int expectSum,vector<int>& path,int& currentSum)
{
//将当前节点值加入路径
currentSum += pNode->m_nValue;
path.push_back(pNode->m_nValue);
//用剪枝法优化,sum值如果大于期望值,下面的路径就不用遍历了,将当前节点从路径中删除并返回
if(currentSum > expectSum)
{
currentSum -= pNode->m_nValue;
path.pop_back();
return ;
}
bool isLeaf = (NULL == pNode->m_pLeft && NULL == pNode->m_pRight);
if(isLeaf && currentSum == expectSum)
{
printf("A path has found:\n");
vector<int>::iterator iter = path.begin();
for(iter; iter!=path.end(); iter++)
printf("%d ",*iter);
printf("\n");
}
if(pNode->m_pLeft != NULL)
BinaryTreeFindPathWithExpectSumCore(pNode->m_pLeft,expectSum,path,currentSum);
if(pNode->m_pRight != NULL)
BinaryTreeFindPathWithExpectSumCore(pNode->m_pRight,expectSum,path,currentSum);
//将当前节点值从路径中删除
currentSum -= pNode->m_nValue;
path.pop_back();
}