Skip to content
This repository was archived by the owner on Dec 12, 2023. It is now read-only.
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
105 changes: 105 additions & 0 deletions solutions/java/BinarySearchTreeCheck.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
/* Uses Inorder traversal. Only works on unique element BT */
public class BinarySearchTreeCheck {
Node root;

public BinarySearchTreeCheck() {
root = null;
}

public class Node {
int value;
Node left;
Node right;

public Node(int value) {
this.value = value;
this.left = null;
this.right = null;
}

public void setLeft(Node left) {
this.left = left;
}

public Node getLeft() {
return left;
}

public void setRight(Node right) {
this.right = right;
}

public Node getRight() {
return right;
}

public int getValue() {
return value;
}
}

public void createBinaryTree(int option) {
root = new Node(10);
Node one = new Node(8);
Node two = new Node(5);
Node three = new Node(9);
Node four = new Node(15);
switch (option) {
case 1: /* Is BST (Only unique elements) */
root.setLeft(one);
root.setRight(four);
one.setLeft(two);
one.setRight(three);
break;
case 2: /* Not BST (Only unique elements) */
root.setRight(two);
root.setLeft(one);
one.setLeft(four);
one.setRight(three);
break;
default:
break;
}
}

public boolean isBSTBetter() {
if (root == null)
return true;
return isBSTBetter(root);
}

private Integer prev = null;

public boolean isBSTBetter(Node cur) {
if (cur == null)
return true;

// Check for the left subtree
if (!isBSTBetter(cur.getLeft())) {
return false;
}

// Check the cur value and update prev
if (prev != null && cur.getValue() <= prev)
return false;
prev = cur.getValue();

// Check for the right subtree
if (!isBSTBetter(cur.getRight())) {
return false;
}

return true;
}

public static void main(String[] args) {
BinarySearchTreeCheck btOne = new BinarySearchTreeCheck();
btOne.createBinaryTree(1);
BinarySearchTreeCheck btTwo = new BinarySearchTreeCheck();
btTwo.createBinaryTree(2);

// Only works if all the elements in the Binary Tree are unique.
System.out.println(btOne.isBSTBetter());
System.out.println(btTwo.isBSTBetter());
}
}