输入一颗二叉树和一个整数,打印出二叉树中结点值的和为输入整数的所有路径。路径定义为从树的根结点开始往下一直到叶结点所经过的结点形成一条路径。
import java.util.ArrayList;
/**
public class TreeNode {
int val = 0;
TreeNode left = null;
TreeNode right = null;
public TreeNode(int val) {
this.val = val;
}
}
*/
public class Solution {
public ArrayList<ArrayList<Integer>> FindPath(TreeNode root,int target) {
ArrayList<ArrayList<Integer>> result = new ArrayList<ArrayList<Integer>>();
ArrayList<Integer> list = new ArrayList<Integer>();
if(root == null) return result;
list.add(root.val);
F(root, target, root.val, result, list);
return result;
}
/**
* result存储各条成功路径,list存储本条成功路径
*/
public static void F(TreeNode root, int target, int now, ArrayList<ArrayList<Integer>> result, ArrayList<Integer>list){
//如果root是叶子,进行该路径之和的判断
if(root.left==null && root.right==null){
if(target == now){
result.add((ArrayList<Integer>)list.clone());
}
}
//将左右孩子分别放入路径继续
else{
if(root.left!=null){
list.add(root.left.val);
F(root.left, target, now + root.left.val, result,list);
list.remove(list.size()-1);
}
if(root.right!=null){
list.add(root.right.val);
F(root.right, target, now + root.right.val, result, list);
list.remove(list.size()-1);
}
}
}
}