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

题目描述

输入一棵二叉树和一个整数,打印出二叉树中节点值的和为输入整数的所有路径。从树的根节点开始往下一直到叶节点所经过的节点形成一条路径。

示例:
给定如下二叉树,以及目标和 target = 22

1
2
3
4
5
6
7
      5
/ \
4 8
/ / \
11 13 4
/ \ / \
7 2 5 1

返回:

1
2
3
4
[
[5,4,11,2],
[5,8,4,5]
]

提示:

  1. 节点总数 <= 10000

解题思路

方法一:深度优先搜索

代码实现

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
class Solution34 {
List<List<Integer>> res = new ArrayList<>();
List<Integer> path = new ArrayList<>();

public List<List<Integer>> pathSum(TreeNode root, int target) {
recur(root, target);
return res;
}

void recur(TreeNode root, int target) {
if (root == null) return;
// if (target < 0) {//剪枝 不能这么操作,本题没有设置结点的值大于0的限制。
// return;
// }
path.add(root.val);
target -= root.val;
if (target == 0 && root.left == null && root.right == null)
res.add(new ArrayList<>(path));
recur(root.left, target);
recur(root.right, target);
path.remove(path.size() - 1);
}
}

资料

Author

John Doe

Posted on

2021-05-28

Updated on

2021-06-13

Licensed under

You need to set install_url to use ShareThis. Please set it in _config.yml.
You forgot to set the business or currency_code for Paypal. Please set it in _config.yml.

Comments

You forgot to set the shortname for Disqus. Please set it in _config.yml.