import java.util.*;
/**
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 expectNumber) {
ArrayList<ArrayList<Integer>> ret = new ArrayList<>() ;
ArrayList<Integer> path = new ArrayList<>() ;
find(root , expectNumber , ret , path) ;
return ret ;
}
public void find(TreeNode root , int exp , ArrayList<ArrayList<Integer>> ret , ArrayList<Integer> path) {
if(root == null) {
return ;
}
if(root.left == null && root.right == null) {
if(exp == root.val) {
path.add(root.val) ;
ArrayList<Integer> temp = new ArrayList<>() ;
for(Integer num : path) {
temp.add(num) ;
}
ret.add(temp) ;
path.remove(path.size()-1) ;
return ;
} else {
return ;
}
} else {
exp-=root.val ;
path.add(root.val) ;
if(root.left != null) {
find(root.left , exp , ret , path) ;
}
if(root.right != null) {
find(root.right , exp , ret , path) ;
}
path.remove(path.size()-1) ;
return ;
}
}
}
/**
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 expectNumber) {
ArrayList<ArrayList<Integer>> ret = new ArrayList<>() ;
ArrayList<Integer> path = new ArrayList<>() ;
find(root , expectNumber , ret , path) ;
return ret ;
}
public void find(TreeNode root , int exp , ArrayList<ArrayList<Integer>> ret , ArrayList<Integer> path) {
if(root == null) {
return ;
}
if(root.left == null && root.right == null) {
if(exp == root.val) {
path.add(root.val) ;
ArrayList<Integer> temp = new ArrayList<>() ;
for(Integer num : path) {
temp.add(num) ;
}
ret.add(temp) ;
path.remove(path.size()-1) ;
return ;
} else {
return ;
}
} else {
exp-=root.val ;
path.add(root.val) ;
if(root.left != null) {
find(root.left , exp , ret , path) ;
}
if(root.right != null) {
find(root.right , exp , ret , path) ;
}
path.remove(path.size()-1) ;
return ;
}
}
}