forked from MiYazJE/Acepta-el-reto
-
Notifications
You must be signed in to change notification settings - Fork 0
/
p201.java
73 lines (58 loc) · 1.84 KB
/
p201.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
package TreeNode;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
class TreeNode {
char val;
TreeNode left, right;
TreeNode(char x) { val = x; }
public String toString() {
return val + "";
}
}
public class p201 {
static void detectTwoSons(TreeNode node) {
if (node == null || !two) return;
if ((node.left != null && node.right == null) || (node.left == null && node.right != null))
two = false;
detectTwoSons(node.left);
detectTwoSons(node.right);
}
static void detectOneSon(TreeNode node) {
if (node == null || !one) return;
if (node.left != null && node.right != null) { one = false; return; }
detectOneSon(node.left);
detectOneSon(node.right);
}
static void read(TreeNode node) {
if (input.charAt(pos) != '.') {
node.left = new TreeNode(input.charAt(pos++));
read(node.left);
}
pos++;
if (input.charAt(pos) != '.') {
node.right = new TreeNode(input.charAt(pos++));
read(node.right);
}
}
static int pos;
static String input;
static boolean one, two;
final static BufferedReader s = new BufferedReader(new InputStreamReader(System.in));
public static void main(String[] args) throws IOException {
while (true) {
input = s.readLine();
pos = 0;
TreeNode node = new TreeNode(input.charAt(pos++));
if (node.val == '.') break;
read(node);
one = two = true;
detectOneSon(node);
detectTwoSons(node);
if (one) System.out.print("1");
if (two) System.out.print("2");
if (!one && !two) System.out.print("N");
System.out.println();
}
}
}