Quick actions

cmd+k|ctrl+k

Navigation

Languages

Validate Binary Search Tree

Snippet info

Language

JavaScript

Visibility

public

Author

anikeshdey2020

Created

2023-03-07T06:49:40.184566Z

Updated

2023-03-07T06:49:40.184566Z

function validate(root){

    if(!root.length){
        return false;
    }

    if(!root[0]){
        return false;
    }
    
    let currentNode = 0;
    let left = 1;
    let right = 2;

    while(right <= root.length){
        if(root[currentNode] > root[left] && root[currentNode] < root[right]){
            currentNode++;
            left = left + 2;
            right = right + 2;
        } else {
            return false;
        }
    }

    return true;
}

validate([5,1,4,34,4,3,6]);
INFO