编辑代码

const log = console.log;

function BinarySearchTreeNode(list) {
  let root
  list.forEach((e) => {
    if(root ==null) {
       root = new TreeNode(e);
      } else {
       instert(root, new TreeNode(e));
      }
  })
  
  function TreeNode(val, left, right) {
    this.val= val;
    this.left = left;
    this.right = right;
  }
  
  
  function instert(node, newNode) {
    if(newNode.val < node.val) {
      if(node.left == null) {
        node.left = newNode;
      } else {
        instert(node.left, newNode);
      }
    } else {
      if(node.right == null){
        node.right = newNode;
      } else {
        instert(node.right, newNode);
      }
    }
  }
  
  return root
}

log(BinarySearchTreeNode([5,3, 7,8, 9]))