-
Notifications
You must be signed in to change notification settings - Fork 96
/
Copy path[10]_2.py
54 lines (47 loc) · 1.37 KB
/
[10]_2.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
class Node:
def __init__(self, number, left_node, right_node):
self.parent = -1
self.number = number
self.left_node = left_node
self.right_node = right_node
def in_order(node, level):
global level_depth, x
level_depth = max(level_depth, level)
if node.left_node != -1:
in_order(tree[node.left_node], level + 1)
level_min[level] = min(level_min[level], x)
level_max[level] = max(level_max[level], x)
x += 1
if node.right_node != -1:
in_order(tree[node.right_node], level + 1)
n = int(input())
tree = {}
level_min = [n]
level_max = [0]
root = -1
x = 1
level_depth = 1
for i in range(1, n + 1):
tree[i] = Node(i, -1, -1)
level_min.append(n)
level_max.append(0)
for _ in range(n):
number, left_node, right_node = map(int, input().split())
tree[number].left_node = left_node
tree[number].right_node = right_node
if left_node != -1:
tree[left_node].parent = number
if right_node != -1:
tree[right_node].parent = number
for i in range(1, n + 1):
if tree[i].parent == -1:
root = i
in_order(tree[root], 1)
result_level = 1
result_width = level_max[1] - level_min[1] + 1
for i in range(2, level_depth + 1):
width = level_max[i] - level_min[i] + 1
if result_width < width:
result_level = i
result_width = width
print(result_level, result_width)