题目描述
输入一颗二叉树的根节点和一个整数,打印出二叉树中结点值的和为输入整数的所有路径。路径定义为从树的根结点开始往下一直到叶结点所经过的结点形成一条路径。(注意: 在返回值的list中,数组长度大的数组靠前)
题解:递归
//因为如果放在里面的话每次递归的时候就会重新new一个res和list,这样会把上一步的结果覆盖 private static ArrayList<Integer> list = new ArrayList<>(); private static ArrayList<ArrayList<Integer>> res = new ArrayList<>(); public static ArrayList<ArrayList<Integer>> FindPath(TreeNode root, int target) { if (root == null) { return res; } //不可以设置中间变量,在递归中不正确 target -= root.val; //先判断再加入 if (target >= 0) { list.add(root.val); if (target == 0 && root.left == null && root.right == null) { //查看源码可知此处创建的对象复制了list中本就存在的元素 res.add(new ArrayList<Integer>(list)); } FindPath(root.left, target); FindPath(root.right, target); //弹出结点,每一轮递归返回到父结点时,当前路径也应该回退一个结点 list.remove(list.size() - 1); } return res; }
源码解析:new ArrayList<Integer>(list)
/** * Constructs a list containing the elements of the specified * collection, in the order they are returned by the collection's * @param c 要将其元素放入此列表中的集合 * @throws NullPointerException if the specified collection is null */ public ArrayList(Collection<? extends E> c) { elementData = c.toArray(); if ((size = elementData.length) != 0) { // c.toArray might (incorrectly) not return Object[] (see 6260652) if (elementData.getClass() != Object[].class) elementData = Arrays.copyOf(elementData, size, Object[].class); } else { // replace with empty array. this.elementData = EMPTY_ELEMENTDATA; } }
初始化:
public static class TreeNode{ int val=0; TreeNode left=null; TreeNode right=null; public TreeNode(int val){ this.val=val; } } public static TreeNode createBinTree(int[] array) { nodeList=new LinkedList<TreeNode>(); // 将一个数组的值依次转换为TreeNode节点 for (int nodeIndex = 0; nodeIndex < array.length; nodeIndex++) { nodeList.add(new TreeNode(array[nodeIndex])); } // 对前lastParentIndex-1个父节点按照父节点与孩子节点的数字关系建立二叉树 for (int parentIndex = 0; parentIndex < array.length / 2 - 1; parentIndex++) { // 左孩子 nodeList.get(parentIndex).left = nodeList .get(parentIndex * 2 + 1); // 右孩子 nodeList.get(parentIndex).right = nodeList .get(parentIndex * 2 + 2); } // 最后一个父节点:因为最后一个父节点可能没有右孩子,所以单独拿出来处理 int lastParentIndex = array.length / 2 - 1; // 左孩子 nodeList.get(lastParentIndex).left = nodeList .get(lastParentIndex * 2 + 1); // 右孩子,如果数组的长度为奇数才建立右孩子 if (array.length % 2 == 1) { nodeList.get(lastParentIndex).right = nodeList .get(lastParentIndex * 2 + 2); } return nodeList.get(0); }
测试:
private static List<TreeNode> nodeList = null; public static void main(String[] args) { int[] tree={10,5,12,4,7}; int target=22; TreeNode node = createBinTree(tree); res= FindPath(node, target); System.out.println(res); } 输出:[[10, 5, 7], [10, 12]]