Answers for "check if binary search tree is valid"

1

check if binary search tree is valid

class BTNode {
  constructor(value) {
    this.value = value;
    this.left = null;
    this.right = null;
  }
}

/**
 *
 * @param {BTNode} tree
 * @returns {Boolean}
 */
const isBinarySearchTree = (tree) => {
  if (tree) {
    if (
      tree.left &&
      (tree.left.value > tree.value || !isBinarySearchTree(tree.left))
    ) {
      return false;
    }
    if (
      tree.right &&
      (tree.right.value <= tree.value || !isBinarySearchTree(tree.right))
    ) {
      return false;
    }
  }
  return true;
};
Posted by: Guest on April-04-2021
0

how to check if a binary tree is a BST

1    def checkBST(t) 
2        return false if t==nil
3       if t.left!=nil && t.left>t 
4            return false
5        end
6        if t.right!=nil && t.right<t
7            return false
8        end
9        if checkBST(t.left) && checkBST(t.right)
10            return true
11        end
12    end
Posted by: Guest on June-07-2021

Code answers related to "check if binary search tree is valid"

Code answers related to "Javascript"

Browse Popular Code Answers by Language