I am new to programming and am trying to calculate the depth of a binary tree in Python . I believe that my error is because depth is a method of the Node class and not a regula
You've got four cases to consider:
You've covered cases 1 and 4, but missed 2 and 3. The fix:
# Return height of tree rooted at this node.
def depth(self):
if self.left == None and self.right == None:
return 1
elif self.left == None:
return self.right.depth() + 1
elif self.right == None:
return self.left.depth() + 1
else:
return max(self.left.depth(), self.right.depth()) + 1