您的位置:首页 > 其它

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

2013-11-19 22:04 281 查看
/***

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

题目:输入一个整数和一棵二元树。

从树的根结点开始往下访问一直到叶结点所经过的所有结点形成一条路径。

打印出和与输入整数相等的所有路径。

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

10

/ \

5 12

/ \

4 7

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

**/

#include <iostream>
#include <vector>
using namespace std;

struct BTreeNode{
	int m_nvalue;
	BTreeNode * m_pLeft;
	BTreeNode * m_pRight;
};

void addBTreeNode(BTreeNode * & pTree, int value){
	if( NULL == pTree){
		BTreeNode * pBTree = new BTreeNode;
		pBTree->m_nvalue = value;
		pBTree->m_pLeft = NULL;
		pBTree->m_pRight = NULL;
		pTree = pBTree;
	}
	else{
		if(pTree->m_nvalue < value)
			addBTreeNode(pTree->m_pRight,value);
		else if(value < pTree->m_nvalue )
			addBTreeNode(pTree->m_pLeft,value);
		else
			cout << "重复输入" << endl;
	}
}

void findPath(BTreeNode * pCurrent, int expSum, int & currentSum,vector<int> & path){
	if(NULL == pCurrent)
		return;
	currentSum += pCurrent->m_nvalue;
	path.push_back(pCurrent->m_nvalue);

	if(!pCurrent->m_pLeft && !pCurrent->m_pRight){
		if(currentSum == expSum){
			vector<int>::iterator iter = path.begin();
			for(;iter != path.end();++iter)
				cout << *iter << '\t';
			cout << endl;
		}
	}

	if(pCurrent->m_pLeft)
		findPath(pCurrent->m_pLeft,expSum,currentSum,path);
	if(pCurrent->m_pRight)
		findPath(pCurrent->m_pRight,expSum,currentSum,path);

	path.pop_back();
	currentSum -=pCurrent->m_nvalue;

}

int main(){
	int sum = 0;
	vector<int> path;
	BTreeNode * pRoot = NULL;
	addBTreeNode(pRoot,10);
	addBTreeNode(pRoot,5);
	addBTreeNode(pRoot,12);
	addBTreeNode(pRoot,4);
	addBTreeNode(pRoot,7);
	findPath(pRoot,22,sum,path);

	return 0;
}


/***************************************************
***********
题目:输入一个整数和一棵二元树。
从树的根结点开始往下访问一直到叶结点所经过的所有结点形成一条路径。
打印出和与输入整数相等的所有路径。
例如输入整数22和如下二元树
                                            10
                                           /   \
                                          5     12
                                        /   \   
                                      4     7  
则打印出两条路径:10, 12和10, 5, 7。
************
*****************************************************/

#include <iostream>
#include <vector>

using namespace std;

struct BSTreeNode{
	int value;
	BSTreeNode * p_Right;
	BSTreeNode * p_Left;
};

void addBSTreeNode(BSTreeNode * & pCurrent, int value){
	if(!pCurrent){
		BSTreeNode * pBSTreeNode;
		pBSTreeNode = new BSTreeNode;
		pBSTreeNode->value = value;
		pBSTreeNode->p_Left = NULL;
		pBSTreeNode->p_Right = NULL;
		pCurrent = pBSTreeNode;
	}
	else{
		if(pCurrent->value > value)
			addBSTreeNode(pCurrent->p_Left,value);
		else if(pCurrent->value < value)
			addBSTreeNode(pCurrent->p_Right,value);
		else
			cout << "repeated input !" << endl;
	}
}
	
void findPath(BSTreeNode * pCurrent,int sum){
	
	static int tempSum = 0;
	static vector<int> ivec;

	if(!pCurrent)
		return;

	tempSum += pCurrent->value;

	ivec.push_back(pCurrent->value);

	//find the leaf node and the tempSum=sum then output the result
	if(!pCurrent->p_Left && !pCurrent->p_Right && sum==tempSum)
	{
		vector<int>::const_iterator con_iter = ivec.begin();
		for(; con_iter!=ivec.end(); ++con_iter)
		{
			cout << *con_iter << " ";
		}
		cout << endl;

	}
	else{							//goto its its child
		if(pCurrent->p_Left)
			findPath(pCurrent->p_Left,sum);
		if(pCurrent->p_Right)
			findPath(pCurrent->p_Right,sum);
	}
		ivec.pop_back();
		tempSum -= pCurrent->value;
}

int main(){

	BSTreeNode * pRoot = NULL;		//creat a binary search tree
	addBSTreeNode(pRoot,10);
	addBSTreeNode(pRoot,5);
	addBSTreeNode(pRoot,12);
	addBSTreeNode(pRoot,4);
	addBSTreeNode(pRoot,7);
	addBSTreeNode(pRoot,3);

	findPath(pRoot,22);				//find the path

	return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: