BinarySearchTree.prototype.height = function () {
if (this.right === null && this.left === null) return 1;
var heightLeft = 0;
var heightRight = 0;
if(this.left!=null)
heightLeft = this.height(this.left);
if(this.right!=null)
heightRight =this.height(this.right);
if(heightLeft > heightRight){
return heightLeft+1;
}
else{
return heightRight+1;
}
};
I need to get the height of the BST and i see the theory but i want that the root counts as 1 and not as 0 .
So if i had a BST like this :
// 16 ---> lvl1
// / \
// 6 23 ---> lvl2
// / \ / \
// 2 14 17 31 ---> lvl3
// \
// 5 ---> lvl4
As its can be seen, the endheight its going to be 4 in the example.
Sorry for my band inglish