思路:根据前序遍历来,返回上一层的时候需要删除path中的值
static void findPath(Node root, int k) {
List<Integer> path = new ArrayList<>();
findPath(root,k,0, path);
}
static void findPath(Node root, int k, int now, List<Integer> path) {
if(root == null)
return;
path.add(root.value);
now += root.value;
if (root.left == null && root.right == null) {
if (k == now && path.size()!=0)
System.out.println(path);
} else {
if (root.left!=null)
findPath(root.left, k, now, path);
if (root.right!=null)
findPath(root.right, k, now, path);
path.remove(path.size() - 1);
}
}