-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtree_height_submission.py
65 lines (49 loc) · 1.68 KB
/
tree_height_submission.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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# -*- coding: utf-8 -*-
"""
Created on Fri Jan 25 14:17:11 2019
@author: rcurran
"""
import sys
import threading
# In Python, the default limit on recursion depth is rather low,
# so raise it here for this problem. Note that to take advantage
# of bigger stack, we have to launch the computation in a new thread.
sys.setrecursionlimit(10**7) # max depth of recursion
threading.stack_size(2**27) # new thread will get stack of such size
class TreeNode():
child_nodes = []
parent_index = None
def __init__(self, child_nodes, parent_index, index):
self.child_nodes = child_nodes
self.parent_index = parent_index
def compute_height_fast(n, parents):
root = construct_tree(n, parents)
return get_height_recursive(root)
def get_height_recursive(tree):
if not tree:
return 0
if not tree.child_nodes:
return 1
heights = []
for node in tree.child_nodes:
heights.append(1 + get_height_recursive(node))
return max(heights)
def construct_tree(n, parents):
treeNodes = []
for i in range(n):
treeNodes.append(TreeNode([], parents[i], i))
root = None
for i in range(n):
parent_index = treeNodes[i].parent_index
if parent_index == -1:
root = treeNodes[i]
else:
treeNodes[parent_index].child_nodes.append(treeNodes[i])
if root is None:
print("Root is null")
return root
def main():
n = int(input())
parents = list(map(int, input().split()))
print(compute_height_fast(n, parents))
threading.Thread(target=main).start()