forked from MiYazJE/Acepta-el-reto
-
Notifications
You must be signed in to change notification settings - Fork 0
/
p203.java
78 lines (58 loc) · 1.74 KB
/
p203.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
74
75
76
77
78
import java.util.Scanner;
/**
* Suma de árboles p298 p203 (Árboles "binarios")
*/
class Tree {
int val;
Tree right, left;
Tree(int x) { val = x; }
public String toString() {
return String.valueOf( val );
}
}
public class p203 {
final static Scanner s = new Scanner(System.in);
static void insertNodes(Tree tree) {
int valor = s.nextInt();
tree.left = new Tree(valor);
if (valor != -1) insertNodes(tree.left);
valor = s.nextInt();
tree.right = new Tree(valor);
if (valor != -1) insertNodes(tree.right);
}
static Tree mergeTrees(Tree t1, Tree t2) {
if (t1 == null || t1.val == -1) {
return t2;
}
if (t2 == null || t2.val == -1) {
return t1;
}
t1.val += t2.val;
t1.left = mergeTrees(t1.left, t2.left);
t1.right = mergeTrees(t1.right, t2.right);
return t1;
}
static boolean space;
static void printTree(Tree tree) {
if (tree == null) return;
if (!space) { System.out.print(tree.val); space = true; }
else System.out.print(" " + tree.val);
if (tree.val != -1) printTree(tree.left);
if (tree.val != -1) printTree(tree.right);
}
public static void main(String[] args) {
while (true) {
Tree t1 = new Tree( s.nextInt() );
if (t1.val != -1)
insertNodes(t1);
Tree t2 = new Tree( s.nextInt() );
if (t1.val == -1 && t2.val == -1) break;
if (t2.val != -1)
insertNodes(t2);
Tree merged = mergeTrees(t1, t2);
space = false;
printTree( merged );
System.out.println();
}
}
}