标签:
1 import java.util.ArrayList; 2 /** 3 public class TreeNode { 4 int val = 0; 5 TreeNode left = null; 6 TreeNode right = null; 7 8 public TreeNode(int val) { 9 this.val = val; 10 11 } 12 13 } 14 */ 15 public class Solution { 16 public ArrayList<ArrayList<Integer>> FindPath(TreeNode root,int target) { 17 ArrayList<ArrayList<Integer>> res = new ArrayList<ArrayList<Integer>> (); 18 ArrayList<Integer> path = new ArrayList<Integer>() ; 19 search(root ,target,res,path) ; 20 return res ; 21 } 22 private void search(TreeNode root, int target, ArrayList<ArrayList<Integer>> res,ArrayList<Integer> path) { 23 if(root==null){ 24 return ; 25 } 26 target = target - root.val ; 27 if(root.left == null && root.right == null){ 28 if(target == 0){ 29 path.add(root.val); 30 res.add(new ArrayList<Integer>(path)); 31 path.remove(path.size()-1); 32 } 33 return; 34 } 35 path.add(root.val ) ; 36 search(root.left,target,res,path) ; 37 search(root.right,target,res,path) ; 38 path.remove(path.size()-1) ; 39 40 } 41 }
标签:
原文地址:http://www.cnblogs.com/huntertoung/p/4801060.html