determine if a given binary tree is a valid bst
Java
// Java implementation to check if given Binary tree
// is a BST or not
/* Class containing left and right child of current
node and key value*/
class Node
{
int data;
Node left, right;
public Node(int item)
{
data = item;
left = right = null;
}
}
public class BinaryTree
{
// Root of the Binary Tree
Node root;
// To keep tract of previous node in Inorder Traversal
Node prev;
boolean isBST() {
prev = null;
return isBST(root);
}
/* Returns true if given search tree is binary
search tree (efficient version) */
boolean isBST(Node node)
{
// traverse the tree in inorder fashion and
// keep a track of previous node
if (node != null)
{
if (!isBST(node.left))
return false;
// allows only distinct values node
if (prev != null && node.data <= prev.data )
return false;
prev = node;
return isBST(node.right);
}
return true;
}
/* Driver program to test above functions */
public static void main(String args[])
{
BinaryTree tree = new BinaryTree();
tree.root = new Node(4);
tree.root.left = new Node(2);
tree.root.right = new Node(5);
tree.root.left.left = new Node(1);
tree.root.left.right = new Node(3);
if (tree.isBST())
System.out.println("IS BST");
else
System.out.println("Not a BST");
}
}
Also in Java:
- Title
- most common element in list java
- Category
- Java
- Title
- read a mail and its content in java mail api
- Category
- Java
- Title
- Java copy file
- Category
- Java
- Title
- determine if a given binary tree is a valid bst
- Category
- Java
- Title
- java loop through array
- Category
- Java
- Title
- java stack
- Category
- Java
- Title
- @fortawesome/free-solid-svg-icons
- Category
- Java
- Title
- android how to know when snackbar is done
- Category
- Java
- Title
- java console write
- Category
- Java
- Title
- get preference value android
- Category
- Java
- Title
- reading string after double in java
- Category
- Java
- Title
- java - a program to print open ports
- Category
- Java
- Title
- java how to initialize an array
- Category
- Java
- Title
- how to import a scanner in java
- Category
- Java
- Title
- java check if string is number
- Category
- Java
- Title
- java map foreach
- Category
- Java
- Title
- java delay
- Category
- Java
- Title
- money value commas java
- Category
- Java
- Title
- android hide and show bottom navigation
- Category
- Java
- Title
- expression régulière téléphone java
- Category
- Java
- Title
- java.io.WriteAbortedException: writing aborted; java.io.NotSerializableException: java.security.MessageDigest$Delegate
- Category
- Java
- Title
- generic method lambda java
- Category
- Java
- Title
- count the number of words in a string java
- Category
- Java
- Title
- org.springframework.orm.jpa.EntityManagerHolder cannot be cast to org.springframework.orm.hibernate5.SessionHolder
- Category
- Java
- Title
- java script find screen size of device
- Category
- Java
- Title
- lightweight java game library
- Category
- Java
- Title
- convert string into unicode java
- Category
- Java
- Title
- generate all prime number less than n java (fastest method)
- Category
- Java
- Title
- java how to serialize a file into a local server
- Category
- Java
- Title
- how to check if a list is empty java
- Category
- Java
- Title
- changing double space to single in java script
- Category
- Java
- Title
- how to scan a character in java
- Category
- Java
- Title
- run java classpath command line
- Category
- Java
- Title
- java new string array
- Category
- Java
- Title
- how to add objects in array java
- Category
- Java
- Title
- java jaxb unmarshall xml to map
- Category
- Java
- Title
- system.out.println shortcut
- Category
- Java
- Title
- java uuid
- Category
- Java
- Title
- how to find powers in java
- Category
- Java
- Title
- java resource file
- Category
- Java
- Title
- java read integer from text file into array scanner
- Category
- Java
- Title
- bukkit java get player count
- Category
- Java
- Title
- java 8 loop in map
- Category
- Java
- Title
- how to change top of window in java
- Category
- Java
- Title
- write an object java in a file
- Category
- Java
- Title
- jtable fill panel
- Category
- Java
- Title
- java download file from url to string
- Category
- Java
- Title
- in dom parser how to find processing instruction in java
- Category
- Java
- Title
- javafx detect collision
- Category
- Java
- Title
- split method in java
- Category
- Java
- Title
- pascals triangle java
- Category
- Java
- Title
- java for loop add multiple declarations
- Category
- Java
- Title
- test function that call a function javascrip
- Category
- Java
- Title
- java final modifier on method
- Category
- Java
- Title
- dates in java 8
- Category
- Java
- Title
- how get started with LWJGL 3
- Category
- Java
- Title
- java script print date in YYYY-MM-DD format
- Category
- Java
- Title
- java lambda list of objects cast
- Category
- Java
- Title
- read csv java
- Category
- Java
- Title
- java set to list
- Category
- Java
- Title
- spigot disable join message
- Category
- Java
- Title
- arraylist set method
- Category
- Java
- Title
- JavaFX mouse over effe
- Category
- Java
- Title
- java override equals and hashcode
- Category
- Java
- Title
- abstraction in java
- Category
- Java
- Title
- get first 5 characters of string java
- Category
- Java
- Title
- java string to char array
- Category
- Java
- Title
- int java
- Category
- Java
- Title
- java how to put a string sentence in a map
- Category
- Java
- Title
- how to check if in array java
- Category
- Java
- Title
- java map iteration
- Category
- Java
- Title
- java square a number
- Category
- Java
- Title
- split with scquere braket in java
- Category
- Java
- Title
- object orientation in java
- Category
- Java
- Title
- java convert a char[] to string
- Category
- Java
- Title
- action on long press of edit edittext
- Category
- Java
- Title
- javadoc reference general toString
- Category
- Java
- Title
- how to skip a line in java
- Category
- Java
- Title
- eliminar el primer caracter de un string java
- Category
- Java
- Title
- how to write a merge sort array method ni java
- Category
- Java
- Title
- isnumber java
- Category
- Java
- Title
- jbutton open jframe java
- Category
- Java
- Title
- how to detect device javascirpt
- Category
- Java
- Title
- java for each
- Category
- Java
- Title
- converter int array para string java
- Category
- Java
- Title
- java djikstra's algorithm
- Category
- Java
- Title
- Spigot API inventory close
- Category
- Java
- Title
- java read from file
- Category
- Java
- Title
- java loop hashmap
- Category
- Java
- Title
- in java how to compare two strings
- Category
- Java
- Title
- iterate over map keys java
- Category
- Java
- Title
- kotlin jsonobject get nested
- Category
- Java
- Title
- how to make a button disapear on click in javafx
- Category
- Java
- Title
- D/NetworkSecurityConfig: No Network Security Config specified, using platform default
- Category
- Java
- Title
- java nested loop
- Category
- Java
- Title
- java create clas
- Category
- Java
- Title
- not equal java
- Category
- Java
- Title
- org.mapstruct maven dependency
- Category
- Java
- Title
- make a commet in java
- Category
- Java
- Title
- java loop object
- Category
- Java