题目描述
题目链接:https://leetcode-cn.com/problems/path-sum-ii/
Given a binary tree and a sum, find all root-to-leaf paths where each path’s sum equals the given sum.
For example: Given the below binary tree and sum = 22,
5
/
4 8
/ /
11 13 4
/ \ /
7 2 5 1
return [ [5,4,11,2], [5,8,4,5] ]
解题思路
基本的深度优先搜索,思路和Path Sum一致,不过要记录下搜索时的路径,把这个临时路径代入到递归里。
程序实现
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {
List<List<Integer>> result=new ArrayList<List<Integer