Path Sum II
Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given sum.
For example:Given the below binary tree and
sum = 22
,
5
/ \
4 8
/ / \
11 13 4
/ \ / \
7 2 5 1
return
[
[5,4,11,2],
[5,8,4,5]
]
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 public class Solution { 2 public ArrayList<ArrayList<Integer>> pathSum(TreeNode root, int sum) { 3 ArrayList<ArrayList<Integer>> res = new ArrayList<ArrayList<Integer>>(); 4 DFS(root,sum,0,new ArrayList<Integer>(),res); 5 return res; 6 } 7 public void DFS(TreeNode root,int sum,int target, ArrayList<Integer>output,ArrayList<ArrayList<Integer>>res){ 8 if(root==null) return; 9 output.add(root.val); 10 target+=root.val; 11 if(root.left==null && root.right==null &&target==sum){ 12 res.add(new ArrayList<Integer>(output)); 13 } 14 DFS(root.right,sum,target,output,res); 15 DFS(root.left,sum,target,output,res); 16 output.remove(output.size()-1); 17 } 18 }
follow up(what if start from root and stop from any node in the tree?)
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 public class Solution { 2 public ArrayList<ArrayList<Integer>> pathSum(TreeNode root, int sum) { 3 ArrayList<ArrayList<Integer>> res = new ArrayList<ArrayList<Integer>>(); 4 ArrayList<Integer> output = new ArrayList<Integer>(); 5 DFS(root,sum,0,output,res); 6 return res; 7 } 8 public void DFS(TreeNode root,int target,int sum,ArrayList<Integer>output,ArrayList<ArrayList<Integer>>res){ 9 if(root==null) return; 10 output.add(root.val); 11 sum +=root.val; 12 if(sum==target){ 13 ArrayList<Integer> temp = new ArrayList<Integer>(); 14 temp.addAll(output); 15 res.add(temp); 16 if(root.left==null && root.right==null ){ 17 output.remove(output.size()-1); 18 return; 19 } 20 } 21 22 if(root.right!=null) 23 DFS(root.right,target,sum,output,res); 24 if(root.left!=null) 25 DFS(root.left,target,sum,output,res); 26 output.remove(output.size()-1); 27 } 28 }
follow up (what if start and stop from any node in the tree?)
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 public ArrayList<ArrayList<Integer>> getPath(TreeNode root){ 2 ArrayList<ArrayList<Integer>> res = new ArrayList<ArrayList<Integer>>(); 3 int path[] = new int[getDepth(root)]; 4 getSum(path,0,10,root,res); 5 return res; 6 } 7 public void getSum(int []path, int level,int target,TreeNode root,ArrayList<ArrayList<Integer>> res){ 8 if(root==null) return; 9 path[level] = root.val; 10 int sum=0; 11 for(int i=level;i>=0;i--){ 12 sum+=path[i]; 13 if(sum==target){ 14 print(path,i,level,res); 15 } 16 } 17 getSum(path,level+1,target,root.left,res); 18 getSum(path,level+1,target,root.right,res); 19 path[level]=Integer.MIN_VALUE; 20 } 21 public int getDepth(TreeNode root){ 22 if(root==null) return 0; 23 return Math.max(getDepth(root.left),getDepth(root.right))+1; 24 } 25 public void print(int[]path,int i,int level,ArrayList<ArrayList<Integer>> res){ 26 ArrayList<Integer> output = new ArrayList<Integer>(); 27 for(int j=i;j<=level;j++){ 28 output.add(path[j]); 29 } 30 res.add(output); 31 }