Skip to content

Commit 558d3ef

Browse files
committed
BST Assignment : Started!
1 parent a03e75c commit 558d3ef

File tree

2 files changed

+120
-1
lines changed

2 files changed

+120
-1
lines changed

binarySearchTree/CheckBSTOrNot.java

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,19 @@
22

33
import static binarySearchTree.SearchInBST.levelWiseInput;
44

5+
6+
class IsBSTReturn {
7+
int minimum;
8+
int maximum;
9+
boolean isBST;
10+
11+
public IsBSTReturn(int minimum, int maximum, boolean isBST) {
12+
this.minimum = minimum;
13+
this.maximum = maximum;
14+
this.isBST = isBST;
15+
}
16+
}
17+
518
public class CheckBSTOrNot {
619

720
public static boolean checkBST(TreeNode<Integer> root) {
@@ -38,8 +51,25 @@ private static int maximum(TreeNode<Integer> root) {
3851
return Math.max(root.data, Math.max(left, right));
3952
}
4053

54+
public static IsBSTReturn isBSTImproved(TreeNode<Integer> root) {
55+
if (root == null) {
56+
return new IsBSTReturn(Integer.MAX_VALUE, Integer.MIN_VALUE, true);
57+
}
58+
59+
var left = isBSTImproved(root.left);
60+
var right = isBSTImproved(root.right);
61+
62+
int minimum = Math.min(root.data, Math.min(left.minimum, right.minimum));
63+
int maximum = Math.max(root.data, Math.max(left.maximum, right.maximum));
64+
65+
boolean isBST = left.maximum < root.data || right.minimum >= root.data || left.isBST || right.isBST;
66+
67+
return new IsBSTReturn(minimum, maximum, isBST);
68+
}
69+
4170
public static void main(String[] args) {
4271
var root = levelWiseInput();
43-
System.out.println(checkBST(root));
72+
// System.out.println(checkBST(root));
73+
System.out.println(isBSTImproved(root).isBST);
4474
}
4575
}
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
package binarySearchTree.assignment;
2+
3+
import binarySearchTree.TreeNode;
4+
5+
import static binarySearchTree.SearchInBST.levelWiseInput;
6+
/*
7+
Given a BST, convert it into a sorted linked list. You have to return the head of LL.
8+
Input format:
9+
The first and only line of input contains data of the nodes of the tree in level order form. The data of the nodes of the tree is separated by space. If any node does not have left or right child, take -1 in its place. Since -1 is used as an indication whether the left or right nodes exist, therefore, it will not be a part of the data of any node.
10+
Output Format:
11+
The first and only line of output prints the elements of sorted linked list.
12+
Constraints:
13+
Time Limit: 1 second
14+
Sample Input 1:
15+
8 5 10 2 6 -1 -1 -1 -1 -1 7 -1 -1
16+
Sample Output 1:
17+
2 5 6 7 8 10
18+
*/
19+
20+
class ListNode<T> {
21+
T data;
22+
ListNode<T> next;
23+
24+
public ListNode(T data) {
25+
this.data = data;
26+
}
27+
}
28+
29+
class PairListNode {
30+
ListNode<Integer> head;
31+
ListNode<Integer> tail;
32+
}
33+
34+
public class BinarySearchTreeToLinkedList {
35+
36+
public static ListNode<Integer> constructLinkedList(TreeNode<Integer> root) {
37+
return helper(root).head;
38+
}
39+
40+
private static PairListNode helper(TreeNode<Integer> root) {
41+
if (root == null) return new PairListNode();
42+
43+
ListNode<Integer> newNode = new ListNode<>(root.data);
44+
45+
var left = helper(root.left);
46+
var right = helper(root.right);
47+
48+
PairListNode pair = new PairListNode();
49+
50+
if (left.tail != null) {
51+
left.tail.next = newNode;
52+
}
53+
54+
newNode.next = right.head;
55+
56+
if (left.tail != null) {
57+
pair.head = left.head;
58+
} else {
59+
pair.head = newNode;
60+
}
61+
62+
if (right.tail != null) {
63+
pair.tail = right.tail;
64+
} else {
65+
pair.tail = newNode;
66+
}
67+
68+
return pair;
69+
}
70+
71+
private static void printListNode(ListNode<Integer> head) {
72+
System.out.print("[ ");
73+
while (head != null) {
74+
System.out.print(head.data + " -> ");
75+
head = head.next;
76+
}
77+
System.out.println("null ]");
78+
}
79+
80+
public static void main(String[] args) {
81+
var root = levelWiseInput();
82+
83+
System.out.println("Constructing the Linked List...");
84+
var head = constructLinkedList(root);
85+
86+
System.out.println("The Linked List is:");
87+
printListNode(head);
88+
}
89+
}

0 commit comments

Comments
 (0)