-
Notifications
You must be signed in to change notification settings - Fork 15
/
p228.java
71 lines (56 loc) · 1.68 KB
/
p228.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
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
class Arbol {
char val;
Arbol left, right;
Arbol(char x) { val = x; }
public String toString() { return ""+val; }
}
public class p228 {
final static BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
static int index;
static void insertNodes(Arbol node, String input) {
if (index == input.length()) return;
char temp = input.charAt(index++);
if (temp != ' ') {
node.left = new Arbol(temp);
insertNodes(node.left, input);
}
if (index == input.length()) return;
temp = input.charAt(index++);
if (temp != ' ') {
node.right = new Arbol(temp);
insertNodes(node.right, input);
}
}
static void invertTree(Arbol node) {
if (node == null) return;
swap( node );
invertTree(node.left);
invertTree(node.right);
}
static void swap(Arbol tree) {
Arbol temp = tree.left;
tree.left = tree.right;
tree.right = temp;
}
static void printTree(Arbol node) {
if (node == null) return;
System.out.print(node.val);
printTree(node.left);
printTree(node.right);
}
public static void main(String[] args) throws IOException {
String line;
while (true) {
line = reader.readLine();
if (line == null) return;
Arbol tree = new Arbol(line.charAt(0));
index = 1; insertNodes(tree, line);
invertTree(tree);
printTree(tree);
System.out.println();
}
}
}