面试题25:二叉树中和为某一值的路径

这篇博客探讨了如何在二叉树中找到和为目标整数的路径。以给定的二叉树和数值22为例,存在两条路径,分别是10,12和10,5,7。文章深入分析了解题思路和实现方法。" 121769389,174677,使用Cordova创建iOS项目的详细步骤,"['Cordova', '移动开发', 'iOS开发', '前端开发']
摘要由CSDN通过智能技术生成
1.输入一棵二叉树和一个整数,打印出二叉树中节点值的和为输入整数的所有路径,从树的根节点开始往下一直到叶节点所经过的节点所形成的路径。

例如输入图中的二叉树和一个整数值22,会有两条路径,第一条路径10,12,另一条路径10,5,7.


分析过程如下:


通过上面表格的分析可以发现,用前序遍历访问树的节点时,应该将节点添加到路径上,并累加该节点的值,如果该节点为叶节点并且路径中的节点的值的和刚好等于输入的整数,则符合当前的要去,可以打印出来。如果不是叶节点的话,则继续访问它的子节点,当前节点访问结束时,递归函数将自动的回到它的父节点。因此在函数退出时应该在路径上删除当前的的节点并减去当当前节点的值,确保返回的是从根节点到父节点的路径。保存路径的数据结构可以用栈来实现。
源码:
/**
* 功能说明:二叉树的路径
* 作者:K0713
* 日期:2016-7-16
**/
#include<iostream>
#include<vector>
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 = 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 PrintTreeNode(BinaryTreeNode* pNode)
{
	if (pNode != NULL)
	{
		cout << "value of this node is: " << pNode->m_nValue << endl;
		if (pNode->m_pLeft != NULL)
			cout << "value of its left child is: " << pNode->m_pLeft->m_nValue << endl;
		else
			cout << "left child is null.\n";
		if (pNode->m_pRight != NULL)
			cout << "value of its right child is:" << pNode->m_pRight->m_nValue << endl;
		else
			cout << "right child is null.\n";
	}
	else
	{
		cout << "this node is null.\n";
	}
	cout << endl;
}
void PrintTree(BinaryTreeNode* pRoot)
{
	PrintTreeNode(pRoot);
	if (pRoot != NULL)
	{
		if (pRoot->m_pLeft != NULL)
			PrintTree(pRoot->m_pLeft);
		if (pRoot->m_pRight != NULL)
			PrintTree(pRoot->m_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);
	}
}
//树的深度
int TreeDepth(BinaryTreeNode* pRoot)
{
	if (pRoot == NULL)
		return 0;
	int nLeft = TreeDepth(pRoot->m_pLeft);
	int nRight = TreeDepth(pRoot->m_pRight);
	return (nLeft > nRight) ? (nLeft + 1) : (nRight + 1);
}

void FindPath(BinaryTreeNode* pRoot, int expectedSum, std::vector<int>& path, int& currentSum);
//寻找路径
void FindPath(BinaryTreeNode* pRoot, int expectedSum)
{
	if(pRoot == NULL)
		return;

	std::vector<int> path;
	int currentSum = 0;
	FindPath(pRoot, expectedSum, path, currentSum);
}

void FindPath
	(
	BinaryTreeNode*   pRoot,        
	int               expectedSum,  
	std::vector<int>& path,         
	int&              currentSum
	)
{
	currentSum += pRoot->m_nValue;
	path.push_back(pRoot->m_nValue);

	// 如果是叶结点,并且路径上结点的和等于输入的值
	// 打印出这条路径
	bool isLeaf = pRoot->m_pLeft == NULL && pRoot->m_pRight == NULL;
	if(currentSum == expectedSum && isLeaf)
	{
		cout<<"A path is found: ";
		std::vector<int>::iterator iter = path.begin();
		for(; iter != path.end(); ++ iter)
			cout<<*iter<<"\t";
		cout<<endl;
	}

	// 如果不是叶结点,则遍历它的子结点
	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中减去当前结点的值
	currentSum -= pRoot->m_nValue;
	path.pop_back();
} 

int main()
{
	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);

	FindPath(pNode10,22);
	system("PAUSE");
	return 0;
}
结果:




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值