-
Notifications
You must be signed in to change notification settings - Fork 0
/
binaryTree.java
72 lines (62 loc) · 2.1 KB
/
binaryTree.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
import java.util.*;
public class binaryTree {
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode() {}
* TreeNode(int val) { this.val = val; }
* TreeNode(int val, TreeNode left, TreeNode right) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
public class TreeNode {
int val;
TreeNode left;
TreeNode right;
TreeNode() {}
TreeNode(int val) { this.val = val; }
TreeNode(int val, TreeNode left, TreeNode right) {
this.val = val;
this.left = left;
this.right = right;
}
}
public List<List<Integer>> pathSum(TreeNode root, int targetSum) {
List<Integer> list = new ArrayList<>();
List<List<Integer>> listoflist= new ArrayList<>();
int counter = 0;
if(root!=null)
i_path(root, targetSum,listoflist, list,counter);
return listoflist;
}
public static void i_path(TreeNode node,int targetSum,List<List<Integer>> listoflist,List<Integer> list,int counter){
if(isLeafNode(node)){
counter+=node.val;
if(counter==targetSum){
list.add(node.val);
System.out.println(list);
listoflist.add((List<Integer>) ((ArrayList<Integer>) list).clone());
list.remove(list.size()-1);
}
}else{
counter+=node.val;
list.add(node.val);
if(node.left!=null){
i_path(node.left,targetSum,listoflist,list,counter);
}
if(node.right!=null){
i_path(node.right,targetSum,listoflist,list,counter);
}
list.remove(list.size()-1);
}
}
public static boolean isLeafNode(TreeNode node){
return node.left==null && node.right==null;
}
}