-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTree.py
More file actions
40 lines (33 loc) · 763 Bytes
/
Tree.py
File metadata and controls
40 lines (33 loc) · 763 Bytes
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
n = int(input())
parents =list(map(int, input().split()))
tree = [[] for _ in range(n)]
ret = 0
delete_node = int(input())
root = -1
for child in range(n) :
parent = parents[child]
if parent == -1:
root = child # 루트 노드 저장
else:
tree[parent].append(child)
def dfs(node) :
global ret, root
if node == delete_node :
return
if not tree[node] :
ret += 1
return
# 삭제되지않은 노드가 하나도 없으면 리프노드
is_leaf = True
for child in tree[node] :
if child != delete_node :
dfs(child)
is_leaf = False
if is_leaf :
ret += 1
return
if delete_node == root:
print(0)
else:
dfs(root)
print(ret)