Repository navigation
Expand file tree
/
Copy pathSolution1232.java
More file actions
61 lines (52 loc) · 1.75 KB
/
Copy pathSolution1232.java
File metadata and controls
61 lines (52 loc) · 1.75 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
package swea.tree;
import java.io.*;
import java.util.*;
class Node {
public String op;
public int num, left, right;
public Node(String op, int left, int right) {
this.op = op;
this.left = left;
this.right = right;
}
public Node(int num) {
this.op = ".";
this.num = num;
}
}
public class Solution1232 {
static int n;
static String m;
static Node[] node = new Node[1001];
public static double check(int x) {
if (node[x].op.equals("+")) {
return check(node[x].left) + check(node[x].right);
} else if (node[x].op.equals("-")) {
return check(node[x].left) - check(node[x].right);
} else if (node[x].op.equals("*")) {
return check(node[x].left) * check(node[x].right);
} else if (node[x].op.equals("/")) {
return check(node[x].left) / check(node[x].right);
} else {
return node[x].num;
}
}
public static void main(String[] args) throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st;
for (int i = 1; i <= 10; i++) {
n = Integer.parseInt(br.readLine());
for (int j = 1; j <= n; j++) {
st = new StringTokenizer(br.readLine());
st.nextToken();
if (st.countTokens() > 1) {
node[j] = new Node(st.nextToken(), Integer.parseInt(st.nextToken()),
Integer.parseInt(st.nextToken()));
} else {
node[j] = new Node(Integer.parseInt(st.nextToken()));
}
}
System.out.println("#" + i + " " + (int) check(1));
}
}
}