Height of a tree - recursive

Run Settings
LanguagePython
Language Version
Run Command
class Node: def __init__(self, info): self.info = info self.left = None self.right = None self.level = None def __str__(self): return str(self.info) class BinarySearchTree: def __init__(self): self.root = None def create(self, val): if self.root == None: self.root = Node(val) else: current = self.root while True: if val < current.info: if current.left: current = current.left else: current.left = Node(val) break elif val > current.info: if current.right: current = current.right else: current.right = Node(val) break else: break # Enter your code here. Read input from STDIN. Print output to STDOUT ''' class Node: def __init__(self,info): self.info = info self.left = None self.right = None // this is a node of the tree , which contains info as data, left , right ''' def height(node): if node != None: if (node.left or node.right): if node.right != None: rightHeight = 1 + height(node.right) else: rightHeight = 0 if node.left != None: leftHeight = 1 + height(node.left) else: leftHeight = 0 return max(rightHeight, leftHeight) else: return 0 tree = BinarySearchTree() t = int(input()) arr = list(map(int, input().split())) for i in range(t): tree.create(arr[i]) print(height(tree.root)) """ inputs: 7 3 5 2 1 4 6 7 1 15 5 3 1 7 5 4 """
Editor Settings
Theme
Key bindings
Full width
Lines