-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproblem18.java
101 lines (100 loc) · 2.95 KB
/
problem18.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
import java.util.*;
import static java.lang.System.out;
import java.lang.String;
class problem18
{
public static void main (String [] args)
{
node root = makeTree();
out.println(maximumTotal(root));
}
public static int maximumTotal(node root)
{
LinkedList<node> q = new LinkedList<node>();
q.offer(root);
int maximum = -1;
while (q.size() > 0)
{
node inspect = q.remove();
if(inspect.hasLeftChild())
{
node left = inspect.getLeftChild();
left.setWeight(left.getValue()+inspect.getWeight());
q.offer(left);
}
if(inspect.hasRightChild())
{
node right = inspect.getRightChild();
right.setWeight(right.getValue()+inspect.getWeight());
q.offer(right);
}
if(!inspect.hasRightChild() && !inspect.hasLeftChild())
{
if (maximum < inspect.getWeight()) maximum = inspect.getWeight();
}
}
return maximum;
}
public static node makeTree ()
{
//PREPROCESSING, MAKE A PSUEDO-BINARY TREE : MEMORY IS SHARED
Scanner sc = new Scanner(System.in);
String currentLine = sc.nextLine();
Scanner line = new Scanner(currentLine);
node root = new node(line.nextInt());
node current = root;
Queue<node> q = new LinkedList<node>();
q.offer(root);
int counter = 2;
while (sc.hasNext())
{
currentLine = sc.nextLine();
line = new Scanner(currentLine).useDelimiter(" ");
node next = null;
boolean skip = false;
while(line.hasNextInt())
{
if(next == null) { current = q.remove(); }
else { current = next; }
if (!skip){
int left = line.nextInt();
current.makeLeftChild(left);
q.offer(current.getLeftChild());
}
int right = line.nextInt();
current.makeRightChild(right);
q.offer(current.getRightChild());
if(line.hasNextInt())
{
next = q.remove();
next.setLeftChild(current.getRightChild());
skip = true;
}
}
counter++;
}
sc.close();
line.close();
return root;
}
}
class node
{
int k;
int weight;
node r;
node l;
node(int x) { k = x; weight = x; }
node getRightChild() { return r; }
node getLeftChild() { return l; }
boolean hasRightChild() { return !(r == null); }
boolean hasLeftChild() { return !(l == null); }
void setValue(int x) { k = x; }
int getValue () { return k; }
void setWeight(int x) { if (x > weight) { weight = x;} }
int getWeight(){ return weight; }
void makeRightChild(int x) { r = new node(x); }
void makeLeftChild(int x) { l = new node(x); }
void setRightChild(node x) { r = x; }
void setLeftChild(node x) { l = x; }
}