Validate Binary Search Tree

Validate Binary Search Tree

var isValidBST = function(root) {
    
    return dfs(root, -Number.MAX_VALUE, Number.MAX_VALUE);
    
    function dfs(node, leftBound, rightBound){
        if(node == null) return true;
        if(node.val <= leftBound || node.val >= rightBound) return false;
        
        return dfs(node.left,leftBound, node.val) && dfs(node.right,node.val, rightBound);
    }
};
728x90
반응형