I'm newbie in Python and I've some problem. I'm using Python 3.
I've used this logic for binary search trees to get the height of it:
class Node:
def __init__(self, data):
self.right = self.left = None
self.data = data
class Solution:
def insert(self, root, data):
if root is None:
return Node(data)
else:
if data > root.data:
cur = self.insert(root.right, data)
root.right = cur
else:
cur = self.insert(root.left, data)
root.left = cur
return root
def getHeight(self, root):
# Write your code here
if root is None:
return 0
else:
return 1 + max(self.getHeight(root.right), self.getHeight(root.left))
T = int(input())
myTree = Solution()
root = None
for i in range(T):
data = int(input())
root = myTree.insert(root, data)
height = myTree.getHeight(root)
print(height)
With this input:
7
3
5
2
1
4
6
7
The first 7 is the number of nodes.
But I got four instead of three and in the example it is said that the height must be three.
What I am doing wrong?
NOTE: my code is only on getHeight method.