问题就是如何解决在遍历中记录路径,没想到好法子~
copy一个,写的思路很清晰,很简洁。
public class Solution { private ArrayList<ArrayList<Integer>> list = new ArrayList<>(); private ArrayList<Integer> line = new ArrayList<>(); public ArrayList<ArrayList<Integer>> FindPath(TreeNode root, int target) { if (root == null || target < 0) { return list; } target -= root.val; line.add(root.val); if (target == 0 && root.left == null && root.right == null) { list.add(new ArrayList<>(line)); } FindPath(root.left, target); FindPath(root.right, target); line.remove(line.size() - 1); return list; } }