Javascript size on binary search tree -
i have done c++ because can pass in parameter reference. having trouble figuring out how in javascript. need change in code? output 1
this.sizeofbst = function(){ size = 0; return sizehelper(this.root, size); } function sizehelper(node, size){ if(node){ sizehelper(node.left, size); size++; sizehelper(node.right, size); } return size }
numbers cannot passed reference in javascript. instead, have sizehelper
return size , add size total.
function sizehelper(node) { if (node) { return 1 + sizehelper(node.left) + sizehelper(node.right); } return 0; }
then can used like
this.sizeofbst = function() { return sizehelper(this.root); }
Comments
Post a Comment