-
Notifications
You must be signed in to change notification settings - Fork 13
/
BST_FixingTwoNodesOfBST.java
152 lines (128 loc) · 2.72 KB
/
BST_FixingTwoNodesOfBST.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
import java.util.Scanner;
// https://practice.geeksforgeeks.org/problems/fixed-two-nodes-of-a-bst/1
// https://www.geeksforgeeks.org/fix-two-swapped-nodes-of-bst/
public class BST_FixingTwoNodesOfBST {
static Node node1, node2, prev;
public static Node correctBST(Node root) {
node1 = node2 = null;
correctBSTUtil(root);
swap(node1, node2);
node1 = node2 = prev = null; // making them null for the after each test cases case there are static global variables.
return root;
}
private static void correctBSTUtil(Node root) {
if (root == null)
return;
correctBSTUtil(root.left);
if (prev != null && (prev.data > root.data)) {
if (node1 == null) {
node1 = prev;
node2 = root;
} else {
node2 = root;
}
}
prev = root;
correctBSTUtil(root.right);
}
private static void swap(Node node1, Node node2) {
if (node1 == null || node2 == null)
return;
int tempNode = node1.data;
node1.data = node2.data;
node2.data = tempNode;
}
static int flag = 1;
static int c = 0;
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
while (t-- > 0) {
int n = sc.nextInt();
int m = n;
if (n == 0) {
System.out.println(0);
continue;
}
Node root = null;
Node temp = null;
for (int i = 0; i < n; i++) {
int a = sc.nextInt();
int a1 = sc.nextInt();
char lr = sc.next().charAt(0);
if (i == 0) {
root = new Node(a);
temp = new Node(a);
switch (lr) {
case 'L':
root.left = new Node(a1);
temp.left = new Node(a1);
break;
case 'R':
root.right = new Node(a1);
temp.right = new Node(a1);
break;
}
} else {
insert(root, a, a1, lr);
insert(temp, a, a1, lr);
}
}
flag = 1;
c = 0;
root = correctBST(root);
inorder(temp, root);
if (c + 1 == m)
System.out.println(flag);
else
System.out.println("0");
}
sc.close();
}
public static void inorder(Node temp, Node root) {
if (flag == 0) {
return;
}
if (temp == null && root == null)
return;
if (root == null) {
flag = 0;
return;
}
if (temp == null) {
flag = 0;
return;
}
if (temp.data == root.data) {
c++;
}
inorder(temp.left, root.left);
inorder(temp.right, root.right);
}
public static void insert(Node root, int a, int a1, char lr) {
if (root == null) {
return;
}
if (root.data == a) {
switch (lr) {
case 'L':
root.left = new Node(a1);
break;
case 'R':
root.right = new Node(a1);
break;
}
return;
}
insert(root.left, a, a1, lr);
insert(root.right, a, a1, lr);
}
static class Node {
int data;
Node left, right;
Node(int key) {
data = key;
left = right = null;
}
}
}