-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathq4p12.java
More file actions
70 lines (63 loc) · 1.83 KB
/
Copy pathq4p12.java
File metadata and controls
70 lines (63 loc) · 1.83 KB
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
import java.lang.*;
import java.util.*;
public class q4p12{
static int n;
public static void main(String[] args){
LinkedList<Node> llink = new LinkedList<>();
int[] arr = {9,3,3,4,-2,-4,7,-4,7,-1,9,2};
Node root = BuildTree(null, arr, 0, new LinkedList<Node>());
displayPreOrder(root);
System.out.println("-------------");
cal(root, 0, 3);
System.out.println(n);
}
public static void displayPreOrder(Node root){
if(root == null)
return;
displayPreOrder(root.left);
displayPreOrder(root.right);
}
//build tree line by line, should use queue structure
public static Node BuildTree(Node root, int[] arr,int start, LinkedList<Node> llink){
if(start < arr.length){
llink.addLast(new Node(arr[start]));
if(llink.size()==1){
root = llink.peekFirst();
}
if(llink.size() > 1){
Node temp = llink.peekFirst();
if(temp.left == null)
temp.left = llink.peekLast();
else if(temp.right == null){
temp.right = llink.peekLast();
llink.removeFirst();
}
}
}else
return null;
BuildTree(root, arr, ++start, llink);
return root;
}
public static void cal(Node root, int sum, int target){
if(root == null)
return;
if(sum > target)
return;
if(sum == target){
n++;
sum = 0;
return;
}
sum = root.value + sum;
cal(root.left, sum, target);
cal(root.right,sum, target);
}
}
class Node{
Node right = null;
Node left = null;
int value;
public Node(int value){
this.value = value;
}
}