What if i want to find the height of the BST ?
rittamdebnath opened this issue · 3 comments
rittamdebnath commented
Is this the right approach ?
class node:
def getHeight(self):
if self is None:
return -1
return 1 + max(self.leftChild.getHeight(), self.rightChild.getHeight())
class Tree:
def getHeight(self):
if self.root:
return self.root.getHeight()
else:
return -1
moverio commented
Yes, that looks correct to me. I didn't test it, but it looks right.
rittamdebnath commented
But its showing error can you please check it .
joeyajames commented
This seems to work. Sorry for the long delay.
def getHeight(self):
if self.leftChild and self.rightChild:
return 1 + max(self.leftChild.getHeight(), self.rightChild.getHeight())
elif self.leftChild:
return 1 + self.leftChild.getHeight()
elif self.rightChild:
return 1 + self.rightChild.getHeight()
else:
return 1
def getHeight(self):
if self.root:
return self.root.getHeight()
else:
return -1