A Binary Search Tree (BST) is a node-based binary tree data structure that has the following properties.
- The left subtree of a node contains only nodes with keys less than the node’s key.
- The right subtree of a node contains only nodes with keys greater than the node’s key.
- Both the left and right subtrees must also be binary search trees.
- Each node (item in the tree) has a distinct key.

Binary Search Tree
Naive Approach:
The idea is to for each node, check if max value in left subtree is smaller than the node and min value in right subtree greater than the node.
Follow the below steps to solve the problem:
- If the current node is null then return true
- If the value of the left child of the node is greater than or equal to the current node then return false
- If the value of the right child of the node is less than or equal to the current node then return false
- If the left subtree or the right subtree is not a BST then return false
- Else return true
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
struct node {
int data;
struct node* left;
struct node* right;
};
struct node* newNode( int data)
{
struct node* node
= ( struct node*) malloc ( sizeof ( struct node));
node->data = data;
node->left = NULL;
node->right = NULL;
return (node);
}
int maxValue( struct node* node)
{
if (node == NULL) {
return INT16_MIN;
}
int value = node->data;
int leftMax = maxValue(node->left);
int rightMax = maxValue(node->right);
return max(value, max(leftMax, rightMax));
}
int minValue( struct node* node)
{
if (node == NULL) {
return INT16_MAX;
}
int value = node->data;
int leftMax = minValue(node->left);
int rightMax = minValue(node->right);
return min(value, min(leftMax, rightMax));
}
int isBST( struct node* node)
{
if (node == NULL)
return 1;
if (node->left != NULL
&& maxValue(node->left) >= node->data)
return 0;
if (node->right != NULL
&& minValue(node->right) <= node->data)
return 0;
if (!isBST(node->left) || !isBST(node->right))
return 0;
return 1;
}
int main()
{
struct node* root = newNode(4);
root->left = newNode(2);
root->right = newNode(5);
root->left->left = newNode(1);
root->left->right = newNode(3);
if (isBST(root))
printf ( "Is BST" );
else
printf ( "Not a BST" );
return 0;
}
|
C
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
struct node {
int data;
struct node* left;
struct node* right;
};
struct node* newNode( int data)
{
struct node* node
= ( struct node*) malloc ( sizeof ( struct node));
node->data = data;
node->left = NULL;
node->right = NULL;
return (node);
}
int maxValue( struct node* node)
{
if (node == NULL) {
return 0;
}
int leftMax = maxValue(node->left);
int rightMax = maxValue(node->right);
int value = 0;
if (leftMax > rightMax) {
value = leftMax;
}
else {
value = rightMax;
}
if (value < node->data) {
value = node->data;
}
return value;
}
int minValue( struct node* node)
{
if (node == NULL) {
return 1000000000;
}
int leftMax = minValue(node->left);
int rightMax = minValue(node->right);
int value = 0;
if (leftMax < rightMax) {
value = leftMax;
}
else {
value = rightMax;
}
if (value > node->data) {
value = node->data;
}
return value;
}
int isBST( struct node* node)
{
if (node == NULL)
return 1;
if (node->left != NULL
&& maxValue(node->left) > node->data)
return 0;
if (node->right != NULL
&& minValue(node->right) < node->data)
return 0;
if (!isBST(node->left) || !isBST(node->right))
return 0;
return 1;
}
int main()
{
struct node* root = newNode(4);
root->left = newNode(5);
root->right = newNode(6);
if (isBST(root))
printf ( "Is BST" );
else
printf ( "Not a BST" );
getchar ();
return 0;
}
|
Java
import java.io.*;
class GFG {
static class node {
int data;
node left, right;
}
static node newNode( int data)
{
node Node = new node();
Node.data = data;
Node.left = Node.right = null ;
return Node;
}
static int maxValue(node Node)
{
if (Node == null ) {
return Integer.MIN_VALUE;
}
int value = Node.data;
int leftMax = maxValue(Node.left);
int rightMax = maxValue(Node.right);
return Math.max(value, Math.max(leftMax, rightMax));
}
static int minValue(node Node)
{
if (Node == null ) {
return Integer.MAX_VALUE;
}
int value = Node.data;
int leftMax = minValue(Node.left);
int rightMax = minValue(Node.right);
return Math.min(value, Math.min(leftMax, rightMax));
}
static int isBST(node Node)
{
if (Node == null ) {
return 1 ;
}
if (Node.left != null
&& maxValue(Node.left) > Node.data) {
return 0 ;
}
if (Node.right != null
&& minValue(Node.right) < Node.data) {
return 0 ;
}
if (isBST(Node.left) != 1
|| isBST(Node.right) != 1 ) {
return 0 ;
}
return 1 ;
}
public static void main(String[] args)
{
node root = newNode( 4 );
root.left = newNode( 2 );
root.right = newNode( 5 );
root.left.left = newNode( 1 );
root.left.right = newNode( 3 );
if (isBST(root) == 1 ) {
System.out.print( "Is BST" );
}
else {
System.out.print( "Not a BST" );
}
}
}
|
Python3
class Node:
def __init__( self , data):
self .data = data
self .left = None
self .right = None
def maxValue(node):
if node is None :
return 0 ;
leftMax = maxValue(node.left)
rightMax = maxValue(node.right)
value = 0 ;
if leftMax > rightMax:
value = leftMax
else :
value = rightMax
if value < node.data:
value = node.data
return value
def minValue(node):
if node is None :
return 1000000000
leftMax = minValue(node.left)
rightMax = minValue(node.right)
value = 0
if leftMax < rightMax:
value = leftMax
else :
value = rightMax
if value > node.data:
value = node.data
return value
def isBST(node):
if node is None :
return True
if (node.left is not None and maxValue(node.left) > node.data):
return False
if (node.right is not None and minValue(node.right) < node.data):
return False
if (isBST(node.left) is False or isBST(node.right) is False ):
return False
return True
if __name__ = = "__main__" :
root = Node( 4 )
root.left = Node( 2 )
root.right = Node( 5 )
root.left.left = Node( 1 )
root.left.right = Node( 3 )
if isBST(root) is True :
print ( "Is BST" )
else :
print ( "Not a BST" )
|
C#
using System;
using System.Linq;
using System.Collections.Generic;
class GFG {
class node {
public int data;
public node left, right;
};
static node newNode( int data)
{
node node = new node();
node.data = data;
node.left = null ;
node.right = null ;
return node;
}
static int maxValue( node node)
{
if (node == null ) {
return Int16.MinValue;
}
int value = node.data;
int leftMax = maxValue(node.left);
int rightMax = maxValue(node.right);
return Math.Max(value, Math.Max(leftMax, rightMax));
}
static int minValue( node node)
{
if (node == null ) {
return Int16.MaxValue;
}
int value = node.data;
int leftMax = minValue(node.left);
int rightMax = minValue(node.right);
return Math.Min(value, Math.Min(leftMax, rightMax));
}
static int isBST( node node)
{
if (node == null )
return 1;
if (node.left != null
&& maxValue(node.left) > node.data)
return 0;
if (node.right != null
&& minValue(node.right) < node.data)
return 0;
if (isBST(node.left)==0 || isBST(node.right)==0)
return 0;
return 1;
}
public static void Main()
{
node root = newNode(4);
root.left = newNode(2);
root.right = newNode(5);
root.left.left = newNode(1);
root.left.right = newNode(3);
if (isBST(root)==1)
Console.Write( "Is BST" );
else
Console.Write( "Not a BST" );
}
}
|
Javascript
class Node{
constructor(data){
this .data = data;
this .left = null ;
this .right = null ;
}
}
function newNode(data){
let node = new Node(data);
return node;
}
function maxValue(node){
if (node == null ) return Number.MIN_VALUE;
let value = node.data;
let leftMax = maxValue(node.left);
let rightMax = maxValue(node.right);
return Math.max(value, Math.max(leftMax, rightMax));
}
function minValue(node){
if (node == null ) return Number.MAX_VALUE;
let value = node.data;
let leftMax = minValue(node.left);
let rightMax = minValue(node.right);
return Math.min(value, Math.min(leftMax, rightMax));
}
function isBST(node){
if (node == null ) return 1;
if (node.left != null && maxValue(node.left) > node.data)
return 0;
if (node.right != null && minValue(node.right) < node.data)
return 0;
if (!isBST(node.left) || !isBST(node.right))
return 0;
return 1;
}
let root = newNode(4);
root.left = newNode(2)
root.right = newNode(5)
root.left.left = newNode(1)
root.left.right = newNode(3)
if (isBST(root))
console.log( "Is BST" );
else
console.log( "Not a BST" );
|
Note: It is assumed that you have helper functions minValue() and maxValue() that return the min or max int value from a non-empty tree
Time Complexity: O(N2), As we visit every node just once and our helper method also takes O(N) time, so overall time complexity becomes O(N) * O(N) = O(N2)
Auxiliary Space: O(H), Where H is the height of the binary tree, and the extra space is used due to the function call stack.
Check BST using specified range of minimum and maximum values of nodes:
The isBSTUtil() function is a recursive helper function that checks whether a subtree (rooted at a given node) is a BST within the specified range of minimum (min) and maximum (max) values. If any node violates this range, the function returns false; otherwise, it continues checking the left and right subtrees.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
class node {
public :
int data;
node* left;
node* right;
node( int data)
{
this ->data = data;
this ->left = NULL;
this ->right = NULL;
}
};
int isBSTUtil(node* node, int min, int max);
int isBST(node* node)
{
return (isBSTUtil(node, INT_MIN, INT_MAX));
}
int isBSTUtil(node* node, int min, int max)
{
if (node == NULL)
return 1;
if (node->data < min || node->data > max)
return 0;
return isBSTUtil(node->left, min, node->data - 1)
&&
isBSTUtil(node->right, node->data + 1,
max);
}
int main()
{
node* root = new node(4);
root->left = new node(2);
root->right = new node(5);
root->left->left = new node(1);
root->left->right = new node(3);
if (isBST(root))
cout << "Is BST" ;
else
cout << "Not a BST" ;
return 0;
}
|
C
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
struct node {
int data;
struct node* left;
struct node* right;
};
int isBSTUtil( struct node* node, int min, int max);
int isBST( struct node* node)
{
return (isBSTUtil(node, INT_MIN, INT_MAX));
}
int isBSTUtil( struct node* node, int min, int max)
{
if (node == NULL)
return 1;
if (node->data < min || node->data > max)
return 0;
return isBSTUtil(node->left, min, node->data - 1)
&&
isBSTUtil(node->right, node->data + 1,
max);
}
struct node* newNode( int data)
{
struct node* node
= ( struct node*) malloc ( sizeof ( struct node));
node->data = data;
node->left = NULL;
node->right = NULL;
return (node);
}
int main()
{
struct node* root = newNode(4);
root->left = newNode(2);
root->right = newNode(5);
root->left->left = newNode(1);
root->left->right = newNode(3);
if (isBST(root))
printf ( "Is BST" );
else
printf ( "Not a BST" );
getchar ();
return 0;
}
|
Java
class Node {
int data;
Node left, right;
public Node( int item)
{
data = item;
left = right = null ;
}
}
public class BinaryTree {
Node root;
boolean isBST()
{
return isBSTUtil(root, Integer.MIN_VALUE,
Integer.MAX_VALUE);
}
boolean isBSTUtil(Node node, int min, int max)
{
if (node == null )
return true ;
if (node.data < min || node.data > max)
return false ;
return (
isBSTUtil(node.left, min, node.data - 1 )
&& isBSTUtil(node.right, node.data + 1 , max));
}
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" );
}
}
|
Python3
INT_MAX = 4294967296
INT_MIN = - 4294967296
class Node:
def __init__( self , data):
self .data = data
self .left = None
self .right = None
def isBST(node):
return (isBSTUtil(node, INT_MIN, INT_MAX))
def isBSTUtil(node, mini, maxi):
if node is None :
return True
if node.data < mini or node.data > maxi:
return False
return (isBSTUtil(node.left, mini, node.data - 1 ) and
isBSTUtil(node.right, node.data + 1 , maxi))
if __name__ = = "__main__" :
root = Node( 4 )
root.left = Node( 2 )
root.right = Node( 5 )
root.left.left = Node( 1 )
root.left.right = Node( 3 )
if (isBST(root)):
print ( "Is BST" )
else :
print ( "Not a BST" )
|
C#
using System;
public class Node {
public int data;
public Node left, right;
public Node( int item)
{
data = item;
left = right = null ;
}
}
public class BinaryTree {
public Node root;
public virtual bool BST
{
get
{
return isBSTUtil(root, int .MinValue,
int .MaxValue);
}
}
public virtual bool isBSTUtil(Node node, int min,
int max)
{
if (node == null ) {
return true ;
}
if (node.data < min || node.data > max) {
return false ;
}
return (
isBSTUtil(node.left, min, node.data - 1)
&& isBSTUtil(node.right, node.data + 1, max));
}
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.BST) {
Console.WriteLine( "IS BST" );
}
else {
Console.WriteLine( "Not a BST" );
}
}
}
|
Javascript
class Node
{
constructor(item)
{
this .data=item;
this .left= this .right= null ;
}
}
let root;
function isBST()
{
return isBSTUtil(root, Number.MIN_VALUE,
Number.MAX_VALUE);
}
function isBSTUtil(node,min,max)
{
if (node == null )
return true ;
if (node.data < min || node.data > max)
return false ;
return (isBSTUtil(node.left, min, node.data-1) &&
isBSTUtil(node.right, node.data+1, max));
}
root = new Node(4);
root.left = new Node(2);
root.right = new Node(5);
root.left.left = new Node(1);
root.left.right = new Node(3);
if (isBST())
document.write( "IS BST<br>" );
else
document.write( "Not a BST<br>" );
|
Time Complexity: O(N), Where N is the number of nodes in the tree
Auxiliary Space: O(1), if Function Call Stack size is not considered, otherwise O(H) where H is the height of the tree
The idea is to use Inorder traversal of a binary search tree generates output, sorted in ascending order. So generate inorder traversal of the given binary tree and check if the values are sorted or not
Follow the below steps to solve the problem:
- Do In-Order Traversal of the given tree and store the result in a temp array.
- Check if the temp array is sorted in ascending order, if it is, then the tree is BST.
Note: We can avoid the use of an Auxiliary Array. While doing In-Order traversal, we can keep track of previously visited nodes. If the value of the currently visited node is less than the previous value, then the tree is not BST.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
struct Node {
int data;
struct Node *left, *right;
Node( int data)
{
this ->data = data;
left = right = NULL;
}
};
bool isBSTUtil( struct Node* root, Node*& prev)
{
if (root) {
if (!isBSTUtil(root->left, prev))
return false ;
if (prev != NULL && root->data <= prev->data)
return false ;
prev = root;
return isBSTUtil(root->right, prev);
}
return true ;
}
bool isBST(Node* root)
{
Node* prev = NULL;
return isBSTUtil(root, prev);
}
int main()
{
struct Node* root = new Node(3);
root->left = new Node(2);
root->right = new Node(5);
root->left->left = new Node(1);
root->left->right = new Node(4);
if (isBST(root))
cout << "Is BST" ;
else
cout << "Not a BST" ;
return 0;
}
|
Java
import java.io.*;
class GFG {
public static class Node {
public int data;
public Node left, right;
public Node( int data)
{
this .data = data;
left = right = null ;
}
};
static Node prev;
static Boolean isBSTUtil(Node root)
{
if (root != null ) {
if (!isBSTUtil(root.left))
return false ;
if (prev != null && root.data <= prev.data)
return false ;
prev = root;
return isBSTUtil(root.right);
}
return true ;
}
static Boolean isBST(Node root)
{
return isBSTUtil(root);
}
public static void main(String[] args)
{
Node root = new Node( 3 );
root.left = new Node( 2 );
root.right = new Node( 5 );
root.left.left = new Node( 1 );
root.left.right = new Node( 4 );
if (isBST(root))
System.out.println( "Is BST" );
else
System.out.println( "Not a BST" );
}
}
|
Python3
import math
class Node:
def __init__( self , data):
self .data = data
self .left = None
self .right = None
def isBSTUtil(root, prev):
if (root ! = None ):
if (isBSTUtil(root.left, prev) = = False ):
return False
if (prev ! = None and
root.data < = prev.data):
return False
prev = root
return isBSTUtil(root.right, prev)
return True
def isBST(root):
prev = None
return isBSTUtil(root, prev)
if __name__ = = '__main__' :
root = Node( 3 )
root.left = Node( 2 )
root.right = Node( 5 )
root.right.left = Node( 1 )
root.right.right = Node( 4 )
if (isBST(root) = = None ):
print ( "Is BST" )
else :
print ( "Not a BST" )
|
C#
using System;
public class GFG {
public class Node {
public int data;
public Node left, right;
public Node( int data)
{
this .data = data;
left = right = null ;
}
};
static Node prev;
static Boolean isBSTUtil(Node root)
{
if (root != null ) {
if (!isBSTUtil(root.left))
return false ;
if (prev != null && root.data <= prev.data)
return false ;
prev = root;
return isBSTUtil(root.right);
}
return true ;
}
static Boolean isBST(Node root)
{
return isBSTUtil(root);
}
public static void Main(String[] args)
{
Node root = new Node(3);
root.left = new Node(2);
root.right = new Node(5);
root.left.left = new Node(1);
root.left.right = new Node(4);
if (isBST(root))
Console.WriteLine( "Is BST" );
else
Console.WriteLine( "Not a BST" );
}
}
|
Javascript
class Node
{
constructor(data)
{
this .left = null ;
this .right = null ;
this .data = data;
}
}
let prev;
function isBSTUtil(root)
{
if (root != null )
{
if (!isBSTUtil(root.left))
return false ;
if (prev != null && root.data <= prev.data)
return false ;
prev = root;
return isBSTUtil(root.right);
}
return true ;
}
function isBST(root)
{
return isBSTUtil(root);
}
let root = new Node(3);
root.left = new Node(2);
root.right = new Node(5);
root.left.left = new Node(1);
root.left.right = new Node(4);
if (isBST(root))
document.write( "Is BST" );
else
document.write( "Not a BST" );
|
Time Complexity: O(N), Where N is the number of nodes in the tree
Auxiliary Space: O(H), Here H is the height of the tree and the extra space is used due to the function call stack.
Follow the steps to implement the approach:
- While the current node is not null, do the following:
- If the current node does not have a left child, then process the node and move to its right child.
- If the current node has a left child, then find its inorder predecessor (i.e., the rightmost node in its left subtree) and check if it is less than the current node’s value.
- if the predecessor’s right child is null, then set it to point to the current node and move to the current node’s left child.
- If the predecessor’s right child is already pointing to the current node, then reset it to null (to restore the original binary tree structure), process the current node, and move to its right child.
- Repeat steps b and c until the current node is null.
- If the traversal completes without finding any violation of the BST property, then the binary tree is a valid BST
Below is the implementation of the above approach:
C++
#include <iostream>
using namespace std;
struct TreeNode {
int val;
TreeNode* left;
TreeNode* right;
TreeNode( int x) : val(x), left(NULL), right(NULL) {}
};
bool isValidBST(TreeNode* root) {
TreeNode* curr = root;
TreeNode* prev = NULL;
while (curr != NULL) {
if (curr->left == NULL) {
if (prev != NULL && prev->val >= curr->val)
return false ;
prev = curr;
curr = curr->right;
}
else {
TreeNode* pred = curr->left;
while (pred->right != NULL && pred->right != curr)
pred = pred->right;
if (pred->right == NULL) {
pred->right = curr;
curr = curr->left;
}
else {
pred->right = NULL;
if (prev != NULL && prev->val >= curr->val)
return false ;
prev = curr;
curr = curr->right;
}
}
}
return true ;
}
int main() {
TreeNode* root = new TreeNode(4);
root->left = new TreeNode(2);
root->right = new TreeNode(5);
root->left->left = new TreeNode(1);
root->left->right = new TreeNode(3);
if (isValidBST(root))
cout << "The binary tree is a valid BST." << endl;
else
cout << "The binary tree is not a valid BST." << endl;
return 0;
}
|
Java
class TreeNode {
int val;
TreeNode left;
TreeNode right;
TreeNode( int x)
{
val = x;
left = null ;
right = null ;
}
}
class GFG {
public static boolean isValidBST(TreeNode root)
{
TreeNode curr = root;
TreeNode prev = null ;
while (curr != null ) {
if (curr.left
== null ) {
if (prev != null && prev.val >= curr.val)
return false ;
prev = curr;
curr = curr.right;
}
else {
TreeNode pred = curr.left;
while (pred.right != null
&& pred.right != curr)
pred = pred.right;
if (pred.right
== null ) {
pred.right = curr;
curr = curr.left;
}
else {
pred.right = null ;
if (prev != null
&& prev.val >= curr.val)
return false ;
prev = curr;
curr = curr.right;
}
}
}
return true ;
}
public static void main(String[] args)
{
TreeNode root = new TreeNode( 4 );
root.left = new TreeNode( 2 );
root.right = new TreeNode( 5 );
root.left.left = new TreeNode( 1 );
root.left.right = new TreeNode( 3 );
if (isValidBST(root))
System.out.println(
"The binary tree is a valid BST." );
else
System.out.println(
"The binary tree is not a valid BST." );
}
}
|
Python3
class TreeNode:
def __init__( self , val):
self .val = val
self .left = None
self .right = None
def isValidBST(root):
curr = root
prev = None
while curr ! = None :
if curr.left = = None :
if prev ! = None and prev.val > = curr.val:
return False
prev = curr
curr = curr.right
else :
pred = curr.left
while pred.right ! = None and pred.right ! = curr:
pred = pred.right
if pred.right = = None :
pred.right = curr
curr = curr.left
else :
pred.right = None
if prev ! = None and prev.val > = curr.val:
return False
prev = curr
curr = curr.right
return True
root = TreeNode( 4 )
root.left = TreeNode( 2 )
root.right = TreeNode( 5 )
root.left.left = TreeNode( 1 )
root.left.right = TreeNode( 3 )
if isValidBST(root):
print ( "The binary tree is a valid BST." )
else :
print ( "The binary tree is not a valid BST." )
|
C#
using System;
public class TreeNode {
public int val;
public TreeNode left;
public TreeNode right;
public TreeNode( int x) { val = x; }
}
public class Solution {
public bool IsValidBST(TreeNode root) {
TreeNode curr = root;
TreeNode prev = null ;
while (curr != null ) {
if (curr.left == null ) {
if (prev != null && prev.val >= curr.val)
return false ;
prev = curr;
curr = curr.right;
}
else {
TreeNode pred = curr.left;
while (pred.right != null && pred.right != curr)
pred = pred.right;
if (pred.right == null ) {
pred.right = curr;
curr = curr.left;
}
else {
pred.right = null ;
if (prev != null && prev.val >= curr.val)
return false ;
prev = curr;
curr = curr.right;
}
}
}
return true ;
}
}
public class Program {
public static void Main() {
TreeNode root = new TreeNode(4);
root.left = new TreeNode(2);
root.right = new TreeNode(5);
root.left.left = new TreeNode(1);
root.left.right = new TreeNode(3);
Solution solution = new Solution();
if (solution.IsValidBST(root))
Console.WriteLine( "The binary tree is a valid BST." );
else
Console.WriteLine( "The binary tree is not a valid BST." );
}
}
|
Javascript
class TreeNode {
constructor(val) {
this .val = val;
this .left = null ;
this .right = null ;
}
}
function isValidBST(root) {
let curr = root;
let prev = null ;
while (curr != null ) {
if (curr.left == null ) {
if (prev != null && prev.val >= curr.val)
return false ;
prev = curr;
curr = curr.right;
}
else {
let pred = curr.left;
while (pred.right != null && pred.right != curr)
pred = pred.right;
if (pred.right == null ) {
pred.right = curr;
curr = curr.left;
}
else {
pred.right = null ;
if (prev != null && prev.val >= curr.val)
return false ;
prev = curr;
curr = curr.right;
}
}
}
return true ;
}
let root = new TreeNode(4);
root.left = new TreeNode(2);
root.right = new TreeNode(5);
root.left.left = new TreeNode(1);
root.left.right = new TreeNode(3);
if (isValidBST(root))
console.log( "The binary tree is a valid BST." );
else
console.log( "The binary tree is not a valid BST." );
|
OutputThe binary tree is a valid BST.
Time Complexity: O(N), Where N is the number of nodes in the tree.
Auxiliary Space: O(1) , Because we are not using any addition data structure or recursive call stack.