Skip to content

Commit 376bd25

Browse files
committed
Refactor solution for "Binary Tree Level Order Traversal"
1 parent cc59421 commit 376bd25

File tree

2 files changed

+31
-3
lines changed

2 files changed

+31
-3
lines changed

โ€Žalien-dictionary/KwonNayeon.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
"""
2+
๋ฌธ์ œ ์„ค๋ช…:
3+
There is a new alien language which uses the latin alphabet.
4+
However, the order among letters are unknown to you.
5+
You receive a list of non-empty words from the dictionary,
6+
where words are sorted lexicographically by the rules of this new language.
7+
Derive the order of letters in this language.
8+
9+
Conditions:
10+
- You may assume all letters are in lowercase
11+
- At first different letter, if the letter in s precedes the letter in t in the given list order, then the dictionary order of s is less than t
12+
- The dictionary is invalid, if string a is prefix of string b and b is appear before a
13+
- If the order is invalid, return an empty string
14+
- There may be multiple valid order of letters, return the smallest in normal lexicographical order
15+
- The letters in one string are of the same rank by default and are sorted in Human dictionary order
16+
17+
Time Complexity:
18+
-
19+
20+
Space Complexity:
21+
-
22+
"""

โ€Žbinary-tree-level-order-traversal/KwonNayeon.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,14 @@
1010
- ๊ฒฐ๊ณผ ๋ฆฌ์ŠคํŠธ๋Š” ๋ชจ๋“  ๋…ธ๋“œ์˜ ๊ฐ’์„ ์ €์žฅํ•จ
1111
1212
ํ’€์ด๋ฐฉ๋ฒ•:
13-
1. queue์™€ BFS๋ฅผ ํ™œ์šฉํ•˜์—ฌ ๋ ˆ๋ฒจ ์ˆœ์„œ๋กœ ๋…ธ๋“œ๋ฅผ ์ˆœํšŒ
14-
2. ๊ฐ ๋ ˆ๋ฒจ์˜ ๋…ธ๋“œ๋“ค์„ ๋ณ„๋„์˜ ๋ฆฌ์ŠคํŠธ๋กœ ๋ชจ์•„์„œ ๊ฒฐ๊ณผ์— ์ถ”๊ฐ€
15-
3. ๊ฐ ๋…ธ๋“œ๋ฅผ ์ฒ˜๋ฆฌํ•  ๋•Œ ๊ทธ ๋…ธ๋“œ์˜ ์ž์‹๋“ค์„ ํ์— ์ถ”๊ฐ€ํ•˜์—ฌ ๋‹ค์Œ ๋ ˆ๋ฒจ๋กœ ๋„˜์–ด๊ฐ
13+
1. ๋ฃจํŠธ๊ฐ€ ์—†์œผ๋ฉด ๋นˆ ๋ฆฌ์ŠคํŠธ ๋ฐ˜ํ™˜
14+
2. ํ์— ๋ฃจํŠธ ๋„ฃ๊ธฐ
15+
3. while ํ๊ฐ€ ๋นŒ ๋•Œ๊นŒ์ง€:
16+
- ํ˜„์žฌ ๋ ˆ๋ฒจ์˜ ๋…ธ๋“œ ๊ฐœ์ˆ˜ ์ €์žฅ
17+
- ๊ทธ ๊ฐœ์ˆ˜๋งŒํผ ๋…ธ๋“œ๋ฅผ ๊บผ๋ƒ„
18+
- ๋…ธ๋“œ์˜ ๊ฐ’ ์ €์žฅ
19+
- ์ž์‹์ด ์žˆ์œผ๋ฉด ํ์— ์ถ”๊ฐ€
20+
- ๊ฒฐ๊ณผ์— ํ˜„์žฌ ๋ ˆ๋ฒจ์„ ์ถ”๊ฐ€
1621
"""
1722
# Definition for a binary tree node.
1823
# class TreeNode:
@@ -44,3 +49,4 @@ def levelOrder(self, root: Optional[TreeNode]) -> List[List[int]]:
4449
result.append(current_level)
4550

4651
return result
52+

0 commit comments

Comments
ย (0)