# Middle-题目75：113. 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]
]

public class Solution {
public List<List<Integer>> pathSum(TreeNode root, int sum) {
List<List<Integer>> list = new ArrayList<List<Integer>>();
if(root!=null)
backtrack(list, new ArrayList<Integer>(), root, sum);
return list;
}
private void backtrack(List<List<Integer>> list, List<Integer> sublist, TreeNode root, int sum) {

if(sum-root.val == 0 && root.left == null && root.right == null)
if(root.left!=null)
backtrack(list, sublist, root.left, sum-root.val);
if(root.right!=null)
backtrack(list, sublist, root.right, sum-root.val);
sublist.remove(sublist.size()-1);

}
}

3ms,beats 14.57%,众数3ms,51.99%

05-09 1036

05-24 458

08-12 2930

08-01 736

06-18 1063

02-17 510

06-12 1351

06-11 1100

01-08 465

04-14 1.3万