-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathDay22bst.py
35 lines (35 loc) · 1.09 KB
/
Day22bst.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
class Node:
def __init__(self,data):
self.right=self.left=None
self.data = data
class Solution:
def insert(self,root,data):
if root==None:
return Node(data)
else:
if data<=root.data:
cur=self.insert(root.left,data)
root.left=cur
else:
cur=self.insert(root.right,data)
root.right=cur
return root
## MY CODE STARTS HERE(GET HEIGHT FUNCTION#)######################################
def getHeight(self,root):
#Write your code here
leftt=right=0
if (root==None or (root.left==None and root.right==None)):
return 0
else:
leftt=self.getHeight(root.left)
rightt=self.getHeight(root.right)
return 1+max(leftt,rightt)
### MY CODE ENDS HERE #############################################
T=int(raw_input())
myTree=Solution()
root=None
for i in range(T):
data=int(raw_input())
root=myTree.insert(root,data)
height=myTree.getHeight(root)
print height