在二元树中找出和为某一值的所有路径-数据结构

news/2024/6/16 19:43:25 标签: 数据结构

描述:

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

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

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

          10

        / \

       5 12

      / \

     4  7

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

思路:

1、当访问到某一节点时,把该结点的值添加到当前和变量,且把该结点压入栈中。

2、若结点为叶子结点,且当前和变量等于期望的和,则打印栈中的结点值,即为所需的路径。

3、若结点不是叶子结点,继续访问它的左孩子结点,访问它的右孩子结点。

4、删除该结点。包括从当前和变量中减去结点值,从栈中弹出结点值。此时,已回到父结点。

程序中的栈,利用STL中的vector,这样简化了编码工作。

#include <stdio.h>
#include <stdlib.h>
#include <vector>
#include <stack>
#include <iostream>


using namespace std;

typedef struct _BSTNode
{
	int data;
	struct _BSTNode *lchild; 
	struct _BSTNode *rchild; 
}BSTNode;

static int InsertBSTNode(BSTNode *&node, int data)
{
	BSTNode *tmp = NULL;

	if(NULL == node)
	{
		tmp = (BSTNode *)malloc(sizeof(BSTNode));	
		if(NULL == tmp)
		{
			printf("Malloc Error\n");
			return 0;
		}
		tmp->data = data;
		tmp->lchild = tmp->rchild = NULL;
		node = tmp;
		return 1;
	}
	 //create left or right node
	if(node->data > data)
	{
		InsertBSTNode(node->lchild, data);
	}
	else
	{
		InsertBSTNode(node->rchild, data);
	}
	return 1;
}

//非递归写法
static void FindPath(BSTNode *node, int number)
{
	BSTNode *p = node;
	std::stack<BSTNode *> s;
	std::vector<int> path;
	long current_sum = 0;
	vector<int>::iterator iter;


	while(p || !s.empty())
	{
		if(p != NULL)
		{
			s.push(p);
			path.push_back(p->data);
			current_sum += p->data;
			p = p->lchild;
		}
		else
		{
			p = s.top();
			//printf("%d\n", p->data);
			if(p->rchild == NULL && current_sum == number)
			{
				for(iter = path.begin(); iter != path.end(); ++iter)
				{
					printf("%d ", *iter);
				}
				putchar('\n');
			}
			if(p->rchild == NULL)
			{
				current_sum -= p->data;
				path.pop_back();
			}
			s.pop();
			p = p->rchild;
		}
	}
}

//递归写法,中序遍历
static void FindPathRecursive(BSTNode *node, std::vector<int> &path, int number, int ¤t_sum)
{
	if(node != NULL)
	{
		path.push_back(node->data);
		current_sum += node->data;

		FindPathRecursive(node->lchild, path, number, current_sum);

		//printf("current_sum = %d,%d\n", current_sum, node->data);
		if(node->rchild == NULL && number == current_sum)
		{
			vector<int>::iterator iter;
			for(iter = path.begin(); iter != path.end(); ++iter)
			{
				printf("%d ", *iter);
			}
			putchar('\n');
		}
		if(node->rchild == NULL)
		{
			current_sum -= node->data;
			path.pop_back();
		}
		FindPathRecursive(node->rchild,  path, number, current_sum);
	}
}


int main()
{
	int number = 0;
	BSTNode *root = NULL;
	int current_sum=0;
	//Create Binary Squence Tree
	InsertBSTNode(root, 10);
	InsertBSTNode(root, 12);
	InsertBSTNode(root, 5);
	InsertBSTNode(root, 7);
	InsertBSTNode(root, 4);
	//number
	scanf("%d", &number);
	FindPath(root, number);
	//recursive
	std::vector<int> path;
	FindPathRecursive(root, path, number, current_sum);
	return 0;
}



http://www.niftyadmin.cn/n/1460928.html

相关文章

O2OO是一个汽车故障诊断工具

1简介 O2OO是一个汽车故障诊断工具&#xff0c;通过标准的传感器模块&#xff08;OBD-II&#xff09;。它从汽车的传感器中读取数据写入sqlite数据库。它能转存这些数据值&#xff0c;它能生成数据图表。能够通过这些图表信息分析车辆的行为。0200也能生成一些更深层的数据文件…

数据设计+实践+SQL优化

1数据库设计原则 1.1 原始单据与实体之间的关系   可以是一对一、一对多、多对多的关系。在一般情况下&#xff0c;它们是一对一的关系&#xff1a;即一张原始单据对应且只对应一个实体。 在特殊情况下&#xff0c;它们可能是一对多或多对一的关系&#xff0c;即一张原始单…

You can't specify target table X for update in FROM clause

mysql中遇到的问题&#xff0c; mysql中You cant specify target table <tbl> for update in FROM clause错误的意思是说&#xff0c;不能先select出同一表中的某些值&#xff0c;再update这个表(在同一语句中)。 例如下面这个sql&#xff1a; delete from tbl where id …

Mysql数据库DML操作delete不支持表的别名操作

SQL语句如下&#xff1a; delete from copyspy.iptables a where a.ip in (select b.ip from copyspy.iploginlog b where b.mac ?);这条SQL语句放到Oracle数据库中去执行是可以正常执行的&#xff0c;但是放到MySQL数据库中执行时就出现了如下的错误&#xff1a; [Err] 1064…

XSS攻击的危害

盗取各类用户帐号&#xff0c;如机器登录帐号、用户网银帐号、各类管理员帐号; 控制企业数据&#xff0c;包括读取、篡改、添加、删除企业敏感数据的能力; 盗窃企业重要的具有商业价值的资料; 非法转账; 强制发送电子邮件; 网站挂马; 控制受害者机器向其它网站发起攻击;

CentOS6.5基于snort+barnyard2+base的 入侵检测系统的搭建

CentOS6.5基于snortbarnyard2base的 入侵检测系统的搭建 免责声明 转载参考&#xff1a;www.cnblogs.com/qiubibi/p/4115375.html作者的安装步骤实践。有部分修改。 提供的网盘&#xff1a;&#xff1a;链接&#xff1a;http://pan.baidu.com/s/1bnz0hkz 密码&#xff1a;f…

LAMPSecurity渗透演练

LAMPSecurity渗透演练 免责声明&#xff1a; 仅用于渗透练习&#xff0c;维护互联网安全。 一、练习平台搭建 采用最简单的方式&#xff0c;从https://sourceforge.net/projects/lampsecurity/下载镜像文件&#xff0c;加载到虚拟机。 安装www.backtrack-linux.org攻击平台…

Snort的匹配算法-也是cpu计算占用大的地方

转载&#xff1a;http://www.cnblogs.com/hfww/archive/2012/05/11/2495245.html Snort模式匹配检测 常见的网络攻击方式是在命令中带有特定的攻击代码。Snort的基本原理 是把经过处理的数据包和定义好的规则相匹配&#xff0c;来判断是否有入侵发生。在Snort 检测引擎中&am…