Data Structures - Check if Tree is Binary Search Tree

Hi,

At this moment, to debug an recursion problem… I still use console.log( )to see the true/false return.
It this good practice or common practice ? It took quite a long time for me. Wish to know if there is other convenient way. Thanks :smiley:

Your code so far

var displayTree = (tree) => console.log(JSON.stringify(tree, null, 2));
function Node(value) {
  this.value = value;
  this.left = null;
  this.right = null;
}
function BinarySearchTree() {
  this.root = null;
}
function isBinarySearchTree(tree) {
  // Only change code below this line

  console.log(`####### Begin binary search Tree #######`)
  function isSubTreeBinary(node){
    displayTree(node)
    if(! node){
      console.log(true)
      return true
    }
    if(node.left && node.left.value >= node.value){
      console.log(false)
      return false
    }
    if(node.right && node.right.value <= node.value){
      console.log(false)
      return false
    }
    let x = (
      isSubTreeBinary(node.left) &&
      isSubTreeBinary(node.right)
    )
    console.log(x)
    return x

  }
  return isSubTreeBinary(tree.root)

  // Only change code above this line
}


Challenge: Data Structures - Check if Tree is Binary Search Tree

Link to the challenge:

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.