Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 91f1f18

Browse files
[Silver III] Title: Binary tree, Time: 56 ms, Memory: 34944 KB -BaekjoonHub
1 parent f8f2072 commit 91f1f18

File tree

2 files changed

+87
-0
lines changed

2 files changed

+87
-0
lines changed
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# 13237 Binary tree
2+
from collections import deque
3+
N = int(input())
4+
tree = {k: [] for k in range(1, N+1)}
5+
level = [0]*(N+1)
6+
root = 0
7+
8+
for i in range(1, N+1):
9+
node = int(input())
10+
if(node == -1): root = i
11+
else:
12+
tree[node].append(i)
13+
14+
level[root] = 0
15+
current_lv = 0
16+
17+
q = deque([(root, 0)])
18+
while q:
19+
node, lv = q.popleft()
20+
children = tree[node]
21+
for child in children:
22+
level[child] = lv+1
23+
q.append((child, lv+1))
24+
25+
for i in range(1, len(level)): print(level[i])
26+
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
# [Silver III] Binary tree - 13237
2+
3+
[문제 링크](https://www.acmicpc.net/problem/13237)
4+
5+
### 성능 요약
6+
7+
메모리: 34944 KB, 시간: 56 ms
8+
9+
### 분류
10+
11+
깊이 우선 탐색, 그래프 이론, 그래프 탐색, 트리
12+
13+
### 제출 일자
14+
15+
2025년 4월 11일 23:03:10
16+
17+
### 문제 설명
18+
19+
<p>A binary tree is a mathematical structure made of nodes where each node can have up to two children nodes. One child node will be called left child and the other right child. ch If node B is a child node of A, then we say that A is the parent node of B. In a binary tree, there is only one node that has no parent and we call this node the root of the tree. We call the height of a node N to the distance in nodes between the node N and the root node. The root node’s height is 0.</p>
20+
21+
<p>In this problem, you’ll have to compute the heights of every node of the tree. Each node will be identified by an integer from 1 to the number of nodes n.</p>
22+
23+
<p><img alt="" src="https://onlinejudgeimages.s3-ap-northeast-1.amazonaws.com/problem/13237/1.png" style="float:right; height:169px; width:250px">Check the following tree:</p>
24+
25+
<p>The root of the tree is 1. The left child of 1 is 2, the right child of 1 is 3.</p>
26+
27+
<p>The nodes 4, 5, 6 and 7 do not have any child.</p>
28+
29+
<p>The heights of the nodes are:</p>
30+
31+
<ul>
32+
<li>Node 1: 0</li>
33+
<li>Nodes 2 and 3: 1</li>
34+
<li>Nodes 4, 5, 6 and 7: 2</li>
35+
</ul>
36+
37+
<p><img alt="" src="https://onlinejudgeimages.s3-ap-northeast-1.amazonaws.com/problem/13237/2.png" style="float:right; height:250px; width:228px">The following tree is a bit different:</p>
38+
39+
<p>Node 1 is still the root and has 2 and 3 as left and right children but 3 only have right child. On the contrary, node 4 only has left child (5).</p>
40+
41+
<p>The heights:</p>
42+
43+
<ul>
44+
<li>Node 1: 0</li>
45+
<li>Nodes 2 and 3: 1</li>
46+
<li>Node 4: 2</li>
47+
<li>Node 5: 3</li>
48+
</ul>
49+
50+
### 입력
51+
52+
<p>The first line of the input will contain the number of nodes n. (1 ≤ n ≤ 20)</p>
53+
54+
<p>The following n lines will contain one integer each representing the parent of a node. That is, the second line of the input will contain the parent of node 1, the third line the parent of node 2, etc.</p>
55+
56+
<p>The root node will be identified by -1. Remember that node 1 won’t always be the root node.</p>
57+
58+
### 출력
59+
60+
<p>Print n lines. The first line should be the height of node 1, the second should be the height of node 2, and so on.</p>
61+

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /