Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions 102-Binary-Tree-Level-Order-Traversal/note.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# 102. Binary Tree Level Order Traversal

https://leetcode.com/problems/binary-tree-level-order-traversal/

## step1(まず通す)

幅優先探索をする時に階層ごとにループを回せばいい。

ノード数 n に対して時間計算量 O(n)、空間計算量 O(n)

ノード数の上限は 2000 であり各ノードについて val の取得、子の None 判定とリストへの append などを行う程度なので、計算回数は `2000 * 10 = 2 * 10 ^ 4`くらい、計算時間は 数十 ms くらいか。

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍


## step2(整形&他の人のコードを読む)

- https://github.com/shintaro1993/arai60/pull/30/changes
- この方もそうだし、問題タイトルや文でも level という語が使われている(し、level のほうがよく聞く気がする)ので layer -> level に変数名を変更
- https://github.com/naoto-iwase/leetcode/pull/30/changes#diff-40c0807c5c71abb50c5c8ebe78ffc21551f9ab6d80ccd6a156c960d1febd1882R19
- 「キューにはNoneでないことを確かめていれる方が経験で見通しよく書けるので、そうする。」
- 見通しの他に、None を探索詰めないことで探索回数が半分程度に減るというメリットもありそう。
- https://github.com/naoto-iwase/leetcode/pull/30/changes#diff-40c0807c5c71abb50c5c8ebe78ffc21551f9ab6d80ccd6a156c960d1febd1882R20
- 「計算量 Time: O(V + E) = O(V), Space: O(width) = O(V)」
- 自分では全部のノードを見るから O(V)、と雑に考えていたが、実際には高さの回数ループが回り、その中で同じ高さのノードの数だけループが回るという構造であることに気をつける必要がある
Comment on lines +21 to +22

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

自分もわりとO(V)と大雑把に考えがちなので,勉強になりました.

- https://github.com/tom4649/Coding/pull/25/changes
- 変数名 traversal はちょっと変な気がするので、この方が使っている `values_by_level` とか他の方が書いていたような `level_by_level` がいいかもしれない

## step3(10分以内にさっとかける \* 3回)

https://github.com/MA-yo-TA/leetcode/pull/22/changes#diff-dce60424cb6c268dbcb886bdef92379cfa43ce43bf656d9733daae9cc937f12dR71 で書いてあることと逆をやっているが、今回は values があるため、next_layer も含めてループ内の一時変数という感覚になった。まだ自分の好みが定まってないように感じる。
31 changes: 31 additions & 0 deletions 102-Binary-Tree-Level-Order-Traversal/step1.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
from typing import Optional


class TreeNode:
def __init__(self, val=0, left=None, right=None):
self.val = val
self.left = left
self.right = right


class Solution:
def levelOrder(self, root: Optional[TreeNode]) -> list[list[int]]:
if root is None:
return []

nodes: list[TreeNode] = [root]
traversal: list[list[int]] = []

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

traversal という変数名はすでに言及されている通り,すこし違和感がありましたが,後続で改善されていますね

while nodes:
values: list[int] = []
next_layer: list[TreeNode] = []
for node in nodes:
values.append(node.val)
if node.left is not None:
next_layer.append(node.left)
if node.right is not None:
next_layer.append(node.right)

traversal.append(values)
nodes = next_layer

return traversal
31 changes: 31 additions & 0 deletions 102-Binary-Tree-Level-Order-Traversal/step2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
from typing import Optional


class TreeNode:
def __init__(self, val=0, left=None, right=None):
self.val = val
self.left = left
self.right = right


class Solution:
def levelOrder(self, root: Optional[TreeNode]) -> list[list[int]]:
if root is None:
return []

nodes: list[TreeNode] = [root]
values_by_level: list[list[int]] = []
while nodes:
values: list[int] = []
next_level: list[TreeNode] = []
for node in nodes:
values.append(node.val)
if node.left is not None:
next_level.append(node.left)
if node.right is not None:
next_level.append(node.right)

values_by_level.append(values)
nodes = next_level

return values_by_level
31 changes: 31 additions & 0 deletions 102-Binary-Tree-Level-Order-Traversal/step3.py

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

読みやすかったです

Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
from typing import Optional


class TreeNode:
def __init__(self, val=0, left=None, right=None):
self.val = val
self.left = left
self.right = right


class Solution:
def levelOrder(self, root: Optional[TreeNode]) -> list[list[int]]:
if root is None:
return []

nodes = [root]

@skypenguins skypenguins Jul 20, 2026

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

自分なら命名として nodespending_nodes とかにします。

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ありがとうございます。どういうノードかを明確に表現するのもわかりやすいですね。

values_by_level = []
while nodes:
values = []
next_layer = []
for node in nodes:
values.append(node.val)
if node.left is not None:
next_layer.append(node.left)
if node.right is not None:
next_layer.append(node.right)

values_by_level.append(values)
nodes = next_layer

return values_by_level