/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */publicclassSolution{List<List<Integer>>ret=newLinkedList<List<Integer>>();List<Integer>tmp=newLinkedList<Integer>();publicList<List<Integer>>pathSum(TreeNoderoot,intsum){if(root==null)returnret;dfs(root,sum);returnret;}publicvoiddfs(TreeNoderoot,intsum){tmp.add(root.val);if(sum==root.val&&root.left==null&&root.right==null){ret.add(newLinkedList(tmp));return;}if(root.left!=null){dfs(root.left,sum-root.val);tmp.remove(tmp.size()-1);}if(root.right!=null){dfs(root.right,sum-root.val);tmp.remove(tmp.size()-1);}}}