-
Notifications
You must be signed in to change notification settings - Fork 0
Add 102. Binary Tree Level Order Traversal.md #26
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,177 @@ | ||
# step 1 | ||
階層別BFSをすれば良い | ||
|
||
ノードの数をnとして、 | ||
- time complexity: O(n) | ||
- space complexity: O(n) | ||
```python | ||
class Solution: | ||
def levelOrder(self, root: Optional[TreeNode]) -> List[List[int]]: | ||
if root is None: | ||
return [] | ||
|
||
nodes = [root] | ||
result = [] | ||
while nodes: | ||
values_in_this_level = [] | ||
nodes_in_next_level = [] | ||
for node in nodes: | ||
values_in_this_level.append(node.val) | ||
if node.left is not None: | ||
nodes_in_next_level.append(node.left) | ||
if node.right is not None: | ||
Comment on lines
+20
to
+22
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
nodes_in_next_level.append(node.right) | ||
result.append(values_in_this_level) | ||
nodes = nodes_in_next_level | ||
return result | ||
``` | ||
|
||
Noneもとりあえずつっこんであとから処理するタイプの階層別BFS。 | ||
```python | ||
class Solution: | ||
def levelOrder(self, root: Optional[TreeNode]) -> List[List[int]]: | ||
nodes = [root] | ||
result = [] | ||
while True: | ||
nodes = list(filter(None, nodes)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 取り出す前に |
||
if not nodes: | ||
break | ||
values_in_this_level = [] | ||
nodes_in_next_level = [] | ||
for node in nodes: | ||
values_in_this_level.append(node.val) | ||
nodes_in_next_level.append(node.left) | ||
nodes_in_next_level.append(node.right) | ||
result.append(values_in_this_level) | ||
nodes = nodes_in_next_level | ||
return result | ||
``` | ||
|
||
# step 2 | ||
- https://github.com/Mike0121/LeetCode/pull/7/files#r1587410964 | ||
- >resultはもう少し命名頑張っても良いかも。level_ordered_valuesとかどうでしょうか? | ||
- https://github.com/hayashi-ay/leetcode/pull/32/files | ||
- DFS preorder recursive traversal、階層別でないdequeを使ったBFS | ||
|
||
感想:BFS以外の書き方が思いつかなかったが、levelさえトラックしていればDFSでやろうが動作する。 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. DFSは直感に反する感じがして食わず嫌いしてましたが、解けるんですね。勉強になりました。 それと、深さごとに、入る部屋 |
||
|
||
iterative, preorder DFS traversal | ||
- time complexity: O(n) | ||
- space complexity: O(n) | ||
```python | ||
class Solution: | ||
def levelOrder(self, root: Optional[TreeNode]) -> List[List[int]]: | ||
level_ordered_values = [] | ||
nodes_and_levels = [(root, 0)] | ||
while nodes_and_levels: | ||
node, level = nodes_and_levels.pop() | ||
if node is None: | ||
continue | ||
while not len(level_ordered_values) > level: | ||
level_ordered_values.append([]) | ||
level_ordered_values[level].append(node.val) | ||
nodes_and_levels.append((node.right, level + 1)) | ||
nodes_and_levels.append((node.left, level + 1)) | ||
return level_ordered_values | ||
``` | ||
|
||
recursive, preorder DFS traversal, | ||
2パターン書いてみたが、あまり名前がしっくりこない。 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 全体的に私は不満ないです。大規模言語モデルがいい案くれたりします。 |
||
|
||
- time complexity: O(n) | ||
- space complexity: O(n) | ||
```python | ||
class Solution: | ||
def levelOrder(self, root: Optional[TreeNode]) -> List[List[int]]: | ||
def traverse_nodes_in_preorder( | ||
node: Optional[TreeNode], | ||
level: int | ||
) -> None: | ||
if node is None: | ||
return | ||
|
||
nonlocal level_ordered_values | ||
while not len(level_ordered_values) > level: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 条件部分にnotがあると読む時に反転させる一手間で若干読みづらくなるので、notをつけると読みやすくなる状況以外はnotをつけないことをおすすめします。 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 確かに上から下に読むものと考えたらここの否定は分かりにくいですね。 自分の中では、このループを抜けたつぎの行の There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ここwhileである必然性がなさそうで、なぜwhileなんだろうという違和感を感じます。 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. https://discord.com/channels/1084280443945353267/1200089668901937312/1211248049884499988 |
||
level_ordered_values.append([]) | ||
level_ordered_values[level].append(node.val) | ||
traverse_nodes_in_preorder(node.left, level + 1) | ||
traverse_nodes_in_preorder(node.right, level + 1) | ||
|
||
level_ordered_values = [] | ||
traverse_nodes_in_preorder(root, 0) | ||
return level_ordered_values | ||
``` | ||
|
||
```python | ||
class Solution: | ||
def levelOrder(self, root: Optional[TreeNode]) -> List[List[int]]: | ||
def collect_values_in_preorder( | ||
level_ordered_values: List[List[int]], | ||
node: Optional[TreeNode], | ||
level: int | ||
) -> List[List[int]]: | ||
if node is None: | ||
return level_ordered_values | ||
while not len(level_ordered_values) > level: | ||
level_ordered_values.append([]) | ||
level_ordered_values[level].append(node.val) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. この書き方「階層化していないBFS」でも使っていますが、便利ですね。 |
||
collect_values_in_preorder( | ||
level_ordered_values, | ||
node.left, | ||
level + 1 | ||
) | ||
collect_values_in_preorder( | ||
level_ordered_values, | ||
node.right, | ||
level + 1 | ||
) | ||
return level_ordered_values | ||
return collect_values_in_preorder([], root, 0) | ||
``` | ||
|
||
階層化していないBFS | ||
```python | ||
from collections import deque | ||
|
||
|
||
class Solution: | ||
def levelOrder(self, root: Optional[TreeNode]) -> List[List[int]]: | ||
level_ordered_values = [] | ||
nodes_and_levels = deque([(root, 0)]) | ||
while nodes_and_levels: | ||
node, level = nodes_and_levels.popleft() | ||
if node is None: | ||
continue | ||
while not len(level_ordered_values) > level: | ||
level_ordered_values.append([]) | ||
level_ordered_values[level].append(node.val) | ||
nodes_and_levels.append((node.left, level + 1)) | ||
nodes_and_levels.append((node.right, level + 1)) | ||
return level_ordered_values | ||
``` | ||
|
||
# step 3 | ||
|
||
結局最初の書き方に戻った | ||
|
||
```python | ||
class Solution: | ||
def levelOrder(self, root: Optional[TreeNode]) -> List[List[int]]: | ||
if root is None: | ||
return [] | ||
|
||
level_ordered_values = [] | ||
nodes_in_level = [root] | ||
while nodes_in_level: | ||
nodes_in_next_level = [] | ||
values_in_level = [] | ||
for node in nodes_in_level: | ||
values_in_level.append(node.val) | ||
if node.left is not None: | ||
nodes_in_next_level.append(node.left) | ||
if node.right is not None: | ||
nodes_in_next_level.append(node.right) | ||
nodes_in_level = nodes_in_next_level | ||
level_ordered_values.append(values_in_level) | ||
return level_ordered_values | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 好みなのですが、変数名が全体的に長く感じました。 |
||
``` |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
好みの問題ですが変数名の中に前置詞が入ると変数名が長くなりがちなので、それを避けるために
next_level_nodes
みたいにすることもあります