19
loading...
This website collects cookies to deliver better user experience
Given the root of a binary tree, determine if it is a valid binary search tree (BST).
A valid BST is defined as follows:
function isValidBST(root){
const arr = [];
helper(root,arr);
for(let index = 0;index<arr.length-1;index++){
if(arr[index+1]<=arr[index]){
return false;
}
}
return true;
}
function helper(root,arr){
if(!root)
return;
helper(root.left,arr);
arr.push(root.val);
helper(root.right,arr);
}
arr
.helper(root,arr)
which internally does :-
root.val
inside the arr
. arr
and for any index if an element is less than or equal to previous element, then we simply return false
. This is because elements should have been strictly increasing as per the requirements. true
.arr
space.var isValidBST = function(root){
const prev = helper(root,null);
return prev.isNotValid ? false : true;
}
function helper(root,prev){
if(!root)
return prev;
prev = helper(root.left,prev);
if(prev && root.val <= prev.val){
prev.isNotValid = true;
}
if(prev?.isNotValid)
return prev;
prev = root;
prev = helper(root.right,prev);
return prev;
}
helper(root,prev)
first (prev
represents previous node) :-
if(!root) return prev
- If the root
is undefined
, we return the prev
element. prev = helper(root.left,prev)
- We will first go through the left subtree for each root
to find
the prev
element.if(prev && root.val <= prev.val){
prev.isNotValid = true;
}
- Once we return from the left subtree , if prev
exists, we compare root.val
and prev.val
to check if current root.val
is less than or equal to prev.val
. If it is, we create a property on prev
by the name of isNotValid
and set it to true
. if(prev?.isNotValid) return prev;
- Next we check if this prev.isNotValid
exists or not and if it
does then we simply return prev
to exit early and not further proceed for subsequent right
subtree. prev = root
- This is how we set the prev
value to root
so that for next node we
can use this prev
value for necessary comparisons. prev = helper(root.right,prev);
- Going through the right subtree for each root
to find
the prev
element.return prev;
- It's essential to return the prev
to the calling function for value to reflect. const prev = helper(root,null);
- Inside isValidBST
, we get the prev
element from
helper(root,null)
.return prev.isNotValid ? false : true;
- If prev.isNotValid
exists then that means the BST is invalid and we return false
else we return true
. var isValidBST = function(root){
return helper(root,-Infinity,Infinity);
}
function helper(root,leftMax,rightMax){
if(!root)
return true;
if(root.val > leftMax && root.val < rightMax) {
return helper(root.left,leftMax,root.val) && helper(root.right,root.val,rightMax);
}
return false;
}
helper(root,prev)
:-
if(!root) return true;
- If the root
is undefined
we can say that the BST
is valid till now.if(root.val > leftMax && root.val < rightMax) {
return helper(root.left,leftMax,root.val) && helper(root.right,root.val,rightMax);
}
- This is the core logic where we compare root.val
with leftMax
and rightMax
. Only if root.val
is greater than leftMax
and root.val
is less than rightMax
, we can proceed further to check for corresponding left subtree and right subtree and it's required that both of the subtrees need to return true
for the BST to be valid. In case of left subtree, rightMax
will change to current root.val
and in case of right subtree, leftMax
will change to current root.val
.false
. isValidBST
, we do return helper(root,-Infinity,Infinity);
and pass leftMax
as -Infinity
and rightMax
as Infinity
as initial values for our root
node.