-
Notifications
You must be signed in to change notification settings - Fork 93
/
HeightOfTree.py
42 lines (38 loc) · 878 Bytes
/
HeightOfTree.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
36
37
38
39
40
41
42
import sys
sys.setrecursionlimit(int(1e6))
class node:
def __init__(self, data):
self.data = data
self.children = []
def takeinput(lst):
import queue
q = queue.Queue()
ind = 0
rootval = lst[ind]
ind += 1
if rootval == -1:
return None
root = node(rootval)
q.put(root)
while not q.empty():
curr = q.get()
n = lst[ind]
ind += 1
for i in range(n):
child = lst[ind]
nn = node(child)
curr.children.append(nn)
q.put(nn)
ind += 1
return root
def findheight(root):
if root == None:
return 0
mx=0
for i in root.children:
small_height = findheight(i)
mx=max(mx,small_height)
return 1 + mx
lst = list(map(int, input().split()))
root = takeinput(lst)
print(findheight(root))