找出二叉树中和为某值的所有路径

本文介绍如何在二叉树中寻找从根节点出发,节点值之和等于给定目标值的路径。通过递归方法,当节点值等于目标值时返回路径,若节点值大于目标值则在左右子树中寻找剩余和,若节点值小于目标值则无法找到符合条件的路径。

问题是这样的,在一棵以root为根的树中,找出以root为起点的所有路径,要求路径中节点的值相加等于给定的数值。这个问题很简单,甚至于想都不用想就知道遍历树一遍就可以解决。

找出符合要求的路径,其实就是下面这个递归表达式:
1.求出以root为节点和等于v的路径;
2.如果root->value等于v,那么找到一条路径,返回;
3.如果root->value大于v,那么就分别找出以root左右孩子为根的和等于v – root->value的路径;
4.如果root->value小于v,那么就不存在符合条件的路径。
代码表示如下。

#include <iostream>
#include <sstream>
#include <string>
using namespace std;

struct tree {
	int value;
	struct tree *lchild;
	struct tree *rchild;
};

struct tree *insert(struct tree *root, int value)
{
	if (root) {
		if (value < root->value) {
			root->lchild = insert(root->lchild, value);
		} else {
			root->rchild = insert(root->rchild, value);
		}
	} else {
		root = new struct tree;
		root->value = value;
		root->lchild = 0;
		root->rchild = 0;
	}
	return root;
}

void print_tree_prefix(struct tree *root, string pfx)
{
	if (root) {
		if (root->lchild) {
			cout << pfx << "|" << endl;
			if (root->rchild) {
				cout << pfx << "|-<L> " << root->lchild->value << endl;
				print_tree_prefix(root->lchild, pfx + "|     ");
		    	} else {
				cout << pfx << "`-<L> " << root->lchild->value << endl;
			}
		}
		if (root->rchild) {
		    cout << pfx << "|" << endl;
		    cout << pfx << "`-<R> " << root->rchild->value << endl;
		    print_tree_prefix(root->rchild, pfx + "      ");
		}
	}
}

void print_tree(struct tree *root)
{
	if (root) {
	    cout << root->value << endl;
	    print_tree_prefix(root, "");
	}
}

string itos(int i)
{
	string str;
	stringstream s;
	s << i;
	s >> str;
	return str;
}

void sum_prefix(struct tree *root, int v, string pfx)
{
	if (root) {
		if (v == root->value) {
			cout << pfx << root->value << endl;
		} else if (v > root->value) {
			v -= root->value;
			pfx += itos(root->value) + "+";
			sum_prefix(root->lchild, v, pfx);
			sum_prefix(root->rchild, v, pfx);
		}
	}
}

void sum(struct tree *root, int v)
{
	if (root) {
		sum_prefix(root, v, itos(v) + "=");
	}
}

int main()
{
	struct tree *root = 0;
	root = insert(root,  10);
	root = insert(root,   5);
	root = insert(root,   4);
	root = insert(root,   7);
	root = insert(root,  12);
	root = insert(root,  23);
	root = insert(root,   1);
	root = insert(root,   6);
	root = insert(root,   2);
	print_tree(root);
	sum(root, 22);
	return 0;
}


JZ34题目要求我们 在给定的二叉树找出所有为指定路径。这道题是JZ24二叉树中和为某路径的加强版,因为它需要我们找出所有符合要求的路径,而不是从根节点到叶节点的路径。 我们可以采用深度优先遍历(DFS)的方法来解决这个问题。具体做法如下: 1. 首先我们需要定义个函数,用于遍历二叉树: ```python def dfs(node, target, path, result): # node表示当前遍历的节点 # target表示剩余的目标 # path表示当前已选择的路径 # result表示符合要求的路径列表 # 如果遍历到了空节点,直接返回 if not node: return # 把当前节点添加到路径中 path.append(node.val) # 计算当前剩余的目标 target -= node.val # 如果目标为0,说明找到了条符合要求的路径,将其加入结果列表中 if target == 0 and not node.left and not node.right: result.append(path[:]) # 继续递归遍历左右子树 dfs(node.left, target, path, result) dfs(node.right, target, path, result) # 回溯,将刚刚添加的节点从路径中删除 path.pop() ``` 2. 然后我们需要遍历整个二叉树,对于每个遍历到的节点,都调用次`dfs`函数,将其作为先前路径中的部分,并且更新`target`的: ```python def find_path(root, target): result = [] dfs(root, target, [], result) return result ``` 这样,我们就可以得到所有符合要求的路径了。 总之,这道题需要我们具备二叉树基础知识DFS基础知识,我们需要仔细阅读题目并思考,再将自己的思路转化为代码。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值