Repository navigation
Expand file tree
/
Copy pathTree.java
More file actions
240 lines (223 loc) · 7.18 KB
/
Copy pathTree.java
File metadata and controls
240 lines (223 loc) · 7.18 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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
//Во следната задача треба да изградите бинарно дрво со N јазли, за кое ќе треба да одговорите на Q прашања од видот „колку јазли со точно две деца има во поддрвото на избран јазол“.
//
// Секој јазол ќе има уникатно име. Името на коренот на дрвото секогаш ќе ви биде дадено прво.
//
// Влезот ќе содржи N+Q редови од видот
//
// root ime - Треба да го поставите коренот на дрвото да биде јазелот со име ime
//
// add parent_name child_name - Треба да додадете дете јазел со име child_name на јазелот со име parent_name
//
// ask node_name - Треба да го одговориме прашањето за поддрвото на јазелот со име node_name
//
// Пример и структура на влезот:
//
// Влез
//
// Објаснување
//
// Излез
//
// 11 9
// root bravo
// add bravo echo LEFT
// add echo beard LEFT
// ask beard
// ask bravo
// add bravo foxtrot RIGHT
// add beard hotel LEFT
// add beard india RIGHT
// ask echo
// add foxtrot golf LEFT
// add golf juliet RIGHT
// add india sierra RIGHT
// ask foxtrot
// ask bravo
// ask beard
// add echo mike RIGHT
// add foxtrot tango RIGHT
// ask echo
// ask bravo
// ask foxtrot
// Прикажано е изгледот на дрвото при секоја од 4те групи прашања во влезот
//
//
//
//
//
//
// 0
// 0
// 1
// 0
// 2
// 1
// 2
// 4
// 1
//
//
//
// ------
//
//
//
// In the following task, you need to build a binary tree with N nodes, for which you will need to answer Q questions of the type "how many nodes with exactly two children are there in the subtree of a selected node"..
//
// Each node will have a unique name. The name of the root of the tree will always be given to you first.
//
// The input will contain N+Q rows of the type
//
// root name - You need to set the root of the tree to be the node named name
//
// add parent_name child_name - You need to add a child node named child_name to the node named parent_name
//
// ask node_name - We need to answer the question about the subtree of the node named node_name
//
// Example and structure of the input:
//
// Input
//
// Explanation
//
// Output
//
// 11 9
// root bravo
// add bravo echo LEFT
// add echo beard LEFT
// ask beard
// ask bravo
// add bravo foxtrot RIGHT
// add beard hotel LEFT
// add beard india RIGHT
// ask echo
// add foxtrot golf LEFT
// add golf juliet RIGHT
// add india sierra RIGHT
// ask foxtrot
// ask bravo
// ask beard
// add echo mike RIGHT
// add foxtrot tango RIGHT
// ask echo
// ask bravo
// ask foxtrot
// The tree layout is shown for each of the 4 groups of questions in the sample input.
//
//
//
//
//
//
// 0
// 0
// 1
// 0
// 2
// 1
// 2
// 4
// 1
//
// For example:
//
// Input Result
// 11 9
// root bravo
// add bravo echo LEFT
// add echo beard LEFT
// ask beard
// ask bravo
// add bravo foxtrot RIGHT
// add beard hotel LEFT
// add beard india RIGHT
// ask echo
// add foxtrot golf LEFT
// add golf juliet RIGHT
// add india sierra RIGHT
// ask foxtrot
// ask bravo
// ask beard
// add echo mike RIGHT
// add foxtrot tango RIGHT
// ask echo
// ask bravo
// ask foxtrot
// 0
// 0
// 1
// 0
// 2
// 1
// 2
// 4
// 1
import java.util.*;
class TreeNode {
String name;
TreeNode left, right;
TreeNode(String name) {
this.name = name;
this.left = null;
this.right = null;
}
}
public class Main {
static Map<String, TreeNode> tree = new HashMap<>();
// Метод за броење на јазли со точно две деца
public static int countNodesWithTwoChildren(TreeNode root) {
if (root == null) {
return 0; // Ако јазелот е null, врати 0
}
// Рекурзивно брои ги јазлите со две деца во левото и десното поддрво
int left = countNodesWithTwoChildren(root.left);
int right = countNodesWithTwoChildren(root.right);
// Ако тековниот јазел има точно две деца, додади 1
if (root.left != null && root.right != null) {
return left + right + 1;
}
// Во спротивно, врати го збирот од левото и десното поддрво
return left + right;
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int N = sc.nextInt(); // Број на јазли
int Q = sc.nextInt(); // Број на прашања
sc.nextLine(); // Да се исчисти новиот ред после nextInt()
for (int i = 0; i < N + Q; i++) {
String command = sc.nextLine();
String[] tokens = command.split(" ");
if (tokens[0].equals("root")) {
// Креирање на коренот
TreeNode root = new TreeNode(tokens[1]);
tree.put(tokens[1], root);
} else if (tokens[0].equals("add")) {
// Додавање на дете
String parent = tokens[1];
String child = tokens[2];
String pos = tokens[3];
TreeNode parentNode = tree.get(parent);
if (parentNode != null) {
TreeNode childNode = new TreeNode(child);
if (pos.equals("LEFT")) {
parentNode.left = childNode;
} else if (pos.equals("RIGHT")) {
parentNode.right = childNode;
}
tree.put(child, childNode);
}
} else if (tokens[0].equals("ask")) {
// Прашање за бројот на јазли со две деца
String nodeName = tokens[1];
TreeNode node = tree.get(nodeName);
if (node != null) {
System.out.println(countNodesWithTwoChildren(node));
} else {
System.out.println(0); // Ако јазелот не постои, врати 0
}
}
}
sc.close(); // Затвори го Scanner
}
}