Skip to content

Instantly share code, notes, and snippets.

@cixuuz
Created August 28, 2017 01:10
Show Gist options
  • Save cixuuz/cbc8b7a81a5d6150c51e760786734879 to your computer and use it in GitHub Desktop.
Save cixuuz/cbc8b7a81a5d6150c51e760786734879 to your computer and use it in GitHub Desktop.
[113. Path Sum II] #leetcode
class Solution {
public List<List<Integer>> pathSum(TreeNode root, int sum) {
List<List<Integer>> res = new ArrayList<List<Integer>>();
List<Integer> tmp = new LinkedList<>();
dfs(root, sum, tmp, res);
return res;
}
private void dfs(TreeNode node, int sum, List<Integer> tmp, List<List<Integer>> res) {
if ( node == null ) return;
tmp.add(node.val);
if ( node.left == null && node.right == null && sum == node.val) {
res.add(new LinkedList(tmp));
tmp.remove(tmp.size() - 1);
return;
}
dfs(node.left, sum - node.val, tmp, res);
dfs(node.right, sum - node.val, tmp, res);
tmp.remove(tmp.size()-1);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment