-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathUVa00122_Treesonthelevel.java
More file actions
112 lines (97 loc) · 2.36 KB
/
UVa00122_Treesonthelevel.java
File metadata and controls
112 lines (97 loc) · 2.36 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
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
102
103
104
105
106
107
108
109
110
111
112
package uva;
/* USER: 46724 (sfmunera) */
/* PROBLEM: 58 (122 - Trees on the level) */
/* SUBMISSION: 11535300 */
/* SUBMISSION TIME: 2013-03-31 07:01:35 */
/* LANGUAGE: 2 */
import java.util.*;
public class UVa00122_Treesonthelevel {
static Node root;
static class Node {
int key;
Node left;
Node right;
public Node() {
key = -1;
left = right = null;
}
public void setKey(int k) {
key = k;
}
}
static boolean insert(int k, String path) {
if (path.isEmpty()) {
if (root.key != -1 && root.key != k)
return false;
root.setKey(k);
return true;
}
Node node = root;
for (int i = 0; i < path.length(); ++i) {
if (path.charAt(i) == 'L') {
if (node.left == null)
node.left = new Node();
node = node.left;
} else {
if (node.right == null)
node.right = new Node();
node = node.right;
}
}
if (node.key != -1 && node.key != k)
return false;
node.setKey(k);
return true;
}
static boolean valid(Node node) {
if (node == null) return true;
if (node.key == -1) return false;
if (!valid(node.left) || !valid(node.right))
return false;
return true;
}
static void bfs() {
Queue<Node> Q = new LinkedList<Node>();
ArrayList<Integer> list = new ArrayList<Integer>();
Node node = root;
Q.offer(node);
while (!Q.isEmpty()) {
Node x = Q.poll();
list.add(x.key);
if (x.left != null)
Q.offer(x.left);
if (x.right != null)
Q.offer(x.right);
}
for (int i = 0; i < list.size(); ++i) {
if (i > 0) System.out.print(" ");
System.out.print(list.get(i));
}
System.out.println();
}
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
while (in.hasNext()) {
String token;
ArrayList<String> tokens = new ArrayList<String>();
while (in.hasNext() && !(token = in.next()).equals("()"))
tokens.add(token);
root = new Node();
boolean ok = true;
for (String s : tokens) {
String[] parts = s.split(",");
int k = Integer.parseInt(parts[0].substring(1));
String path = parts[1].substring(0, parts[1].length() - 1);
ok &= insert(k, path);
}
if (ok && !valid(root))
ok = false;
if (!ok)
System.out.println("not complete");
else
bfs();
}
in.close();
System.exit(0);
}
}