4.在二元树中找出和为某一值的所有路径(树)

题目:输入一个整数和棵二元树。
从树的根结点开始往下访问一直到叶所经过有形成条路径。
打印出和与输入整数相等的所有路径。

例如输入整数22 和如下二元树

         10
        / \
       5  12
      / \
     4   7

则打印出两条路径:10, 12 和10, 5, 7。

参考微软100题的答案及http://www.cnblogs.com/qi09/archive/2011/05/24/2055643.html

整理思路如下:

1.创建二叉排序树;2.查找路径,FindPath(BinaryTreeNode *pCurrent,int ExpecteSum,vector<int> &Path,int CurrentSum)。

其中查找路径如下:1.访问到当前结点,把该结点添加到路径上,并累加当前结点的值,即CurrentSum+=pCurrent->m_nValue。

                                     2.判断当前结点是叶子结点,同时ExpecteSum== CurrentSum,则将当前路径打印出来;

                                     3.如果当前结点不是叶子结点,则依次访问该结点的左右孩子结点;

                                     4.当当前结点访问结束后,递归函数将自动回到父结点,故在函数退出前要在路径上删除当前结点并减去当前结点的值,以确保返回父结点时路径刚好是根                                       结点到父结点的路径。

“我们不难看出保存路径的数据结构实际上是一个栈结构,因为路径要与递归调用状态一致,而递归调用本质就是一个压栈和出栈的过程。”

其中好多人都有vector<>来替代栈,但自己对C++并不熟悉,故在此先用下,以后有时间整理下C++。

void push_back(const T&x);//向容器末尾添加一个元素

void pop_back();//弹出容器中最后一个元素(窗口必须非空)

vector<int>::iterator iter;其中iterator表示游标,iter相当于i变量,但输出值为*iter。

#include<stdio.h>
#include<stdlib.h>
#include<vector.h>
struct BinaryTreeNode
{
	int m_nValue; // value of node
	BinaryTreeNode *m_pLeft; //left child of node
	BinaryTreeNode *m_pRight; //rignt child of node
};
void addBTreeNode(BinaryTreeNode *&pCurrent,int key)
{
	if (pCurrent==NULL)
	{
		BinaryTreeNode *pBTree=(BinaryTreeNode *)malloc(sizeof(BinaryTreeNode));
		pBTree->m_nValue=key;
		pBTree->m_pLeft=NULL;
		pBTree->m_pRight=NULL;
		pCurrent=pBTree;
	}
	else
	{
		if ((pCurrent->m_nValue) > key)
		{
			addBTreeNode(pCurrent->m_pLeft,key);
		}
		else
		{
			if ((pCurrent->m_nValue) < key)
			{
				addBTreeNode(pCurrent->m_pRight,key);
			}
			else
			{
				printf("Repeated!\n");
			}
		}
	}
}
void FindPath(BinaryTreeNode *pCurrent, int Expectedsum,vector<int> &path,int Currentsum)
{
	if (pCurrent==NULL)
	{
		return;
	}
	Currentsum+=pCurrent->m_nValue;
	path.push_back(pCurrent->m_nValue);
	//if the node is a leaf, and the sum is same as pre-defined,
	//the path is what we want. print the path
	bool leaf=(pCurrent->m_pLeft==NULL&&pCurrent->m_pRight==NULL);
	if (Expectedsum==Currentsum&&leaf)
	{
		vector<int>::iterator iter;
		for (iter=path.begin(); iter!=path.end(); iter++)
		{
			printf("%d ",*iter);
		}
		printf("\n");
	}
	//if the node is not a leaf, goto its children
	if (pCurrent->m_pLeft)
	FindPath(pCurrent->m_pLeft,Expectedsum,path,Currentsum);
	if (pCurrent->m_pRight)
	FindPath(pCurrent->m_pRight,Expectedsum,path,Currentsum);
	//when we finish visiting a node and return to its parent node,
	//we should delete this node from the path and
	//minus the node's value from the current sum
	Currentsum-=pCurrent->m_nValue;
	path.pop_back();
}
int main()
{
	BinaryTreeNode *root=NULL;
	addBTreeNode(root,10);
	addBTreeNode(root,5);
	addBTreeNode(root,12);
	addBTreeNode(root,4);
	addBTreeNode(root,7);
	vector<int> path;
	FindPath(root,22,path,0);
	system("pause");
}



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值