private ArrayList<ArrayList<Integer>> res;
public ArrayList<ArrayList<Integer>> FindPath(TreeNode root,int target) {
//这不就是树的
res = new ArrayList();
bfs(root,target,new ArrayList<Integer>());
return res;
}
private void bfs(TreeNode root,int target,ArrayList<Integer> ls){
if(null == root){
return;
}
ls.add(root.val);
if(root.val == target && null == root.left && null == root.right){
res.add(new ArrayList<Integer>(ls));
}
bfs(root.left,target-root.val,ls);
bfs(root.right,target - root.val,ls);
ls.remove(ls.size()-1);
}
网友评论