面试题[百度]--二叉排序树中查找和为某一值的所有路径

一 问题描述:

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

如图,输入整数70 和如下二叉排序树

输出为:30 15 25

二 解题思路:

利用二叉树的前序遍历,

注意:路径是从根到叶子的所有节点的序列

#include <iostream>
#include <cstdlib>
#include <vector>
#include <stack>
#include <algorithm>
#include <ctime>
using namespace std;


template<class Type>
class BST;
int N=5;

template<class Type>
class BSTNode 
{
private:
	Type data;
	BSTNode *left;
	BSTNode *right;

public:
	friend class BST<Type>;
	BSTNode() 
	{
	}
	BSTNode(Type data, BSTNode* left = 0, BSTNode* right = 0) 
	{
		this->data = data;
		this->left = left;
		this->right = right;
	}
	BSTNode(const BSTNode& node) 
	{
		this->data = node.data;
		this->left = node.left;
		this->right = node.right;
	}
	BSTNode& operator = (const BSTNode &node) 
	{
		this->data = node.data;
		this->left = node.left;
		this->right = node.right;
		return *this;
	}
	void setData(Type& data) 
	{
		this->data = data;
	}
	Type getData() const 
	{
		return this->data;
	}
	void setLeft(BSTNode *left) 
	{
		this->left = left;
	}
	BSTNode* getLeft() const 
	{
		return this->left;
	}
	void setRight(BSTNode *right) 
	{
		this->right = right;
	}
	BSTNode* getRight() const 
	{
		return this->right;
	}
};


template<class Type>
class BST 
{
private:
	BSTNode<Type>* root; 
	vector<Type> v; 
	stack<Type> Stack; 

	void Insert(BSTNode<Type>*& root, const Type& data) 
	{
		if(!root) 
		{
			root=new BSTNode<Type>(data,0,0);
		}
		else if(root->getData() >= data) 
		{
			Insert(root->left,data);
		}
		else 
		{
			Insert(root->right,data);
		}
	}

	void VisitBST_In_order(BSTNode<Type> *p) 
	{
		if(!p)
		{
		    return;
		}
		
		VisitBST_In_order(p->getLeft());
		cout << p->getData() << " ";
		VisitBST_In_order(p->getRight());
	}
/** No used in this program.
	void SearchTopN(BSTNode<Type>* p) 
	{ 
		static int n;
		if(!p)
		{
		    return;
		}
		else 
		{
			if (n < N) 
			{
			    SearchTopN(p->getRight());
			}
			
			if (++n <= N) 
			{
			    v.push_back(p->getData());
			}
			
			if (n < N) 
			{
			    SearchTopN(p->getLeft());
			}
		}
	}
*/
	bool CheckStack(Type num) 
	{
		stack<Type> temp;
		Type s = 0; 
		bool result;
		
		while (Stack.empty() == false) 
		{
			s += Stack.top();
			temp.push(Stack.top());
			Stack.pop();
		}
		while (temp.empty() == false) 
		{
			Stack.push(temp.top());
			temp.pop();
		}
		
		if (s == num)
		{
		    result = true;
		}
		else
		{
			result = false;
		}
		return result;
	}

	void PrintStack() 
	{
		static int k;
		stack<Type>temp;
		
		while (Stack.empty() == false) 
		{
			temp.push(Stack.top());
			Stack.pop();
		}
		cout << ++k<<"th path is:" << endl;
		while (temp.empty() == false) 
		{
			cout << temp.top() << " ";
			Stack.push(temp.top());
			temp.pop();
		}
		
		cout << endl;
	}

	void PrintPathEqualToAInteger(BSTNode<Type>* p, Type num) 
	{
		if (p) 
		{
			Stack.push(p->getData());
			PrintPathEqualToAInteger(p->left, num);
			PrintPathEqualToAInteger(p->right, num);

			if (!p->getLeft() && !p->getRight() && CheckStack(num) == true)
			{
				PrintStack();
		    }
			if (Stack.empty() == false)
			{
				Stack.pop();
			}
		}
	}

public:
	BST(vector<Type>& input) 
	{
		v.clear();
		typedef typename vector<Type>::iterator iterator;
		iterator it;
		for(it = input.begin(); it != input.end(); it++) {
			v.push_back(*it);
		}
//		random_shuffle(v.begin(),v.end());
		root=0;
	}


	void Create() 
	{
		typedef typename vector<Type>::iterator iterator;
		iterator it;
		for (it = v.begin(); it != v.end(); it++) 
		{
			Insert(root, *it);
		}
	}
	
	void show() 
	{
		VisitBST_In_order(root);
		cout << endl;
	}
	void PrintPathEqualToAInteger(const Type& num) 
	{
		PrintPathEqualToAInteger(root, num);
	}
};


int main() 
{
	//srand(unsigned(time(0)));

	int input[] = {30, 15, 5, 25, 40};
	vector<int> v(input,input+N);
	BST<int> bst(v);
	bst.Create();
	bst.show();

	int num;
	cout << "please input test integer N:";
	cin >> num;
	if(!cin.good()) 
	{
		cerr << "data format error, exit." << endl;
		exit(1);
	}
	bst.PrintPathEqualToAInteger(num);
	
	return 0;
}



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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值