-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathClone_a_Binary_Tree.java
163 lines (139 loc) · 3.54 KB
/
Clone_a_Binary_Tree.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
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
// { Driver Code Starts
import java.util.*;
import java.lang.*;
class Tree {
int data;
Tree left, right, random;
Tree(int d) {
data = d;
left = null;
right = null;
random = null;
}
}
class CloneABT {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int q = sc.nextInt();
int n, n1, n2;
char lr;
Tree root;
while (q-- > 0) {
n = sc.nextInt();
root = null;
Solution g = new Solution();
while (n-- > 0) {
n1 = sc.nextInt();
n2 = sc.nextInt();
lr = sc.next().charAt(0);
if (root == null) {
root = new Tree(n1);
switch (lr) {
case 'L':
root.left = new Tree(n2);
break;
case 'R':
root.right = new Tree(n2);
break;
}
} else {
if (lr == 'X')
clone(root, root, n1, n2);
else
insert(root, n1, n2, lr);
}
}
try {
Tree z = g.cloneTree(root);
if (z == root)
System.out.println("0");
else {
if (printInorder(root, z))
System.out.println("1");
else
System.out.println("0");
}
} catch (ClassCastException ex) {
}
}
}
public static boolean printInorder(Tree a, Tree b) {
if ((a == null) && (b == null))
return true;
if (a != null && b != null) {
boolean check = ((a.data == b.data) && printInorder(a.left, b.left) && printInorder(a.right, b.right));
if (a.random != null && b.random != null)
return (check && (a.random.data == b.random.data));
if (a.random == b.random)
return check;
return false;
}
/*
* if(a.random.data == b.random.data)
* if(printInorder(a.left,b.left)==1)
* if(printInorder(a.right,b.right)==1)
* return 1;
*/
return false;
}
public static void clone(Tree root, Tree proot, int n1, int n2) {
try {
if (root == null && proot == null)
return;
if (n1 == root.data) {
if (proot.data == n2)
root.random = proot;
else {
clone(root, proot.left, n1, n2);
clone(root, proot.right, n1, n2);
}
} else {
clone(root.left, proot, n1, n2);
clone(root.right, proot, n1, n2);
}
} catch (NullPointerException ex) {
}
}
public static void insert(Tree root, int n1, int n2, char lr) {
if (root == null)
return;
if (n1 == root.data) {
switch (lr) {
case 'L':
root.left = new Tree(n2);
break;
case 'R':
root.right = new Tree(n2);
break;
}
} else {
insert(root.left, n1, n2, lr);
insert(root.right, n1, n2, lr);
}
}
}
class Solution {
public Tree cloneTree(Tree tree) {
if (tree == null)
return null;
HashMap<Tree, Tree> map = new HashMap<>();
cloneLeftRight(tree, map);
updateRandom(tree, map);
return map.get(tree);
}
public Tree cloneLeftRight(Tree root, HashMap<Tree, Tree> map) {
if (root == null)
return null;
map.put(root, new Tree(root.data));
map.get(root).left = cloneLeftRight(root.left, map);
map.get(root).right = cloneLeftRight(root.right, map);
return map.get(root);
}
public void updateRandom(Tree root, HashMap<Tree, Tree> map) {
if (map.get(root) == null)
return;
map.get(root).random = map.get(root.random);
updateRandom(root.left, map);
updateRandom(root.right, map);
}
}