###### LeetCode Path Sum II

Description:

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]
]

Solution:

import java.util.*;

public class Solution {
int targetSum;
List<List<Integer>> list = new ArrayList<List<Integer>>();

public List<List<Integer>> pathSum(TreeNode root, int sum) {
if (root == null)
return list;
this.targetSum = sum;
ArrayList<Integer> array = new ArrayList<Integer>();
dfs(root, root.val, array);
return list;
}

void dfs(TreeNode root, int tempSum, ArrayList<Integer> array) {
if (root.left == null && root.right == null) {
if (tempSum == targetSum)
}
if (root.left != null) {
dfs(root.left, tempSum + root.left.val, array);
array.remove(array.size() - 1);
}
if (root.right != null) {
dfs(root.right, tempSum + root.right.val, array);
array.remove(array.size() - 1);
}
}
}

#### Path Sum II -- LeetCode

2014-04-14 23:59:49

#### 【leetcode c++】113 Path Sum II

2015-08-01 00:06:17

#### 【LeetCode-面试算法经典-Java实现】【113-Path Sum II（路径和）】

2015-08-12 06:23:36

#### 【LeetCode】113. Path Sum II 基于Java和C++的解法及分析

2016-05-09 22:25:36

#### leetcode 113. Path Sum II-路径和|回溯算法

2016-06-11 15:35:35

#### 【leetCode】Path Sum II python实现

2016-06-18 21:54:06

#### 【LeetCode with Python】 Path Sum II

2014-06-27 18:02:30

#### leetcode_c++:树: Path SumII（113）

2016-08-25 16:48:12

#### LeetCode详解 之 Path Sum I and II(Java)

2015-05-24 00:55:29

#### LeetCode 113. Path Sum II（路径和）

2016-05-24 00:19:11

## 不良信息举报

LeetCode Path Sum II