二叉树中和为某一值的路径

题目大意:输入一颗二叉树和一个整数,打印出二叉树中结点值的和为输入整数的所有路径。路径定义为从树的根结点开始往下一直到叶结点所经过的结点形成一条路径。

九度OJ链接:http://ac.jobdu.com/problem.php?pid=1368

分析:题目就是先输入一个树,然后在树中找到从根到叶子的路径,使得路径上的节点值之和为给定的某个值。如果有多条,注意题目要求按照字典序输出这些路径。

C++实现:

#include <iostream>
#include <stdio.h>
#include <vector>
#include <memory.h>
#include <string>
#include <algorithm>
#include <iomanip>
#include <map>
#include <fstream>
#include <stack>
#include <queue>
#include <algorithm>
#include <cmath>
using namespace std;

struct Node //树节点定义
{
	int value;
	int id;
	Node *left;
	Node *right;
	int l_index;
	int r_index;
	Node(int val)
	{
		value = val;
		left = NULL;
		right = NULL;
	}
};

void output(vector<int> &path)<span style="white-space:pre">	</span>//输出找到的路径
{
	printf("A path is found:");
	for(int i = 0; i < path.size(); i ++)
	{
		printf(" %d", path[i]);
	}
	printf("\n");
}

int sum;
void getRootToLeafPath(Node *root, int k, vector<int> &path)<span style="white-space:pre">	</span>//先序遍历,查找满足条件的路径
{
	sum += root->value;
	path.push_back(root->id);
	if(root->left == NULL && root->right == NULL)
	{
		if(sum == k)
		{
			output(path);
		}
		sum -= root->value;
		path.pop_back();
		return;
	}
	
	if(root->left)
		getRootToLeafPath(root->left, k, path);
	if(root->right)
		getRootToLeafPath(root->right, k, path);
	sum -= root->value;
	path.pop_back();
}

void input()
{
//	freopen("c:/input.txt","r",stdin);
	int n, k;
	while(scanf("%d%d", &n,&k) != EOF)
	{
		vector<Node*> node;
		Node *root = NULL, *tmp;
		int x, a, b;
		while(n-- > 0)
		{
			scanf("%d%d%d", &x,&a,&b);
			tmp = new Node(x);
			if(a > b)<span style="white-space:pre">	</span>//注意此处调整,为满足输出的字典序要求
				swap(a,b);
			tmp->l_index = a;
			tmp->r_index = b;
			node.push_back(tmp);
		}
		root = node[0];
		
		for(int i = 0; i < node.size(); i ++)
		{
			node[i]->id = i + 1;
			if(node[i]->l_index != -1)
				node[i]->left = node[node[i]->l_index - 1];

			if(node[i]->r_index != -1)
				node[i]->right = node[node[i]->r_index - 1];
		}
		sum = 0;
		vector<int> path;
		printf("result:\n");
		getRootToLeafPath(root, k, path);
//		printf("\n");
	}
}

int main()
{
	input();
	return 0;
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
问题描述: 给定一棵二叉树和一个整数目标值,找出所有从根节点到叶子节点的路径,使得路径上的节点值之和等于目标值。 解题思路: 我们可以使用深度优先搜索(DFS)的思想来解决这个问题。具体步骤如下: 1. 定义一个列表path,用于存储当前的路径。 2. 递归遍历每个节点: a. 将当前节点添加到path。 b. 如果当前节点是叶子节点且路径上的节点值之和等于目标值,则将当前路径添加到结果。 c. 递归遍历当前节点的左子树和右子树。 d. 在递归结束后,将当前节点从path移除,以便开始探索其他路径。 3. 返回结果列表,即所有路径和等于目标值的路径。 代码实现: ``` class Solution: def pathSum(self, root: TreeNode, targetSum: int) -> List[List[int]]: def dfs(node, path, target): if not node: return path.append(node.val) if not node.left and not node.right and sum(path) == target: res.append(path.copy()) dfs(node.left, path, target) dfs(node.right, path, target) path.pop() res = [] dfs(root, [], targetSum) return res ``` 以上代码,我们定义了一个辅助函数dfs来进行递归遍历。在遍历的过程,我们使用列表path来存储当前路径,如果路径上的节点值之和等于目标值,则将当前路径添加到结果列表res。最后返回结果res。 时间复杂度分析: 假设二叉树的节点数为n,则时间复杂度为O(n),因为我们需要遍历每个节点一次。需要注意的是,在每个节点处,我们都会调用sum函数来计算当前路径的节点值之和,因此总的时间复杂度还需要考虑到sum函数的时间复杂度。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值