Skip to content
Open
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
51 changes: 51 additions & 0 deletions binary-tree-level-order-traversal.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
1回目。25分で解けた。

```py
class Solution:
def levelOrder(self, root: Optional[TreeNode]) -> List[List[int]]:
# bfs だねえ、てことはキューか?
if root is None:
return []

queue = [(root, 0)]
res = [[]]

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

変数名が省略した形だと読み手が考えることが増えるので省略しない方が好まれます。
https://google.github.io/styleguide/pyguide.html#316-naming
個人的には result という変数名はこの規模の関数ならOKだと思っています。

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.

コメントありがとうございます!手癖で使ってました...!

while queue:
node, depth = queue.pop(0)

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

list の pop(0) は計算量が長さ分かかるので、collections.deque を使うのが良いと思います。
https://docs.python.org/3/library/collections.html#collections.deque

# 1 level 下のを queue に入れる
if node.left:
queue.append((node.left, depth + 1))
if node.right:
queue.append((node.right, depth + 1))

# res[depth] していいのは、len(res) > depth のとき
if len(res) > depth:
res[depth].append(node.val)
else:
res.append([node.val])
return res
```

2回目。Gemini に聞いて、level ごとに for ループを回したほうが level order traversal としては標準的だし直観的と指摘される。
3回目も同様。

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

あ、Gemini に聞くのいいと思います。
あと、読むのをできればやって欲しくて、たとえば、完走した人の中で気に入った人を見つけておくといいと思います。

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.

重ね重ねすみません。以下の3人のを読むようにします。


```py
from collections import deque
class Solution:

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

class の前には2つ空行を入れるのが一般的と思われます。
https://google.github.io/styleguide/pyguide.html#35-blank-lines
スタイルの問題なので周囲の人と合わせるのが無難です。

def levelOrder(self, root: Optional[TreeNode]) -> List[List[int]]:
if not root:
return []
res = []
queue = deque([root])
while queue:
current_level_vals = []
current_level_size = len(queue)
for _ in range(current_level_size):
node = queue.popleft()
current_level_vals.append(node.val)
if node.left:
queue.append(node.left)

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

この書き方の場合、for ループの途中では今のレベルのノードと次のレベルのノードが存在する点が懸念点としてあるかなと思いました。
先に解いた人のコードやコメント集をみると色々な書き方の幅があって勉強になると感じています。
見たコードがあればそのリンクと思ったことを書いていただくとコメントがもらいやすくなると思います。例えばこの問題だと queue でなく、list を二つ(今のループで見るべきノード、次のループでみるべきノード)使って書くこともできますよね。
https://docs.google.com/document/d/11HV35ADPo9QxJOpJQ24FcZvtvioli770WWdZZDaLOfg/edit?tab=t.0#heading=h.gp4hkfr3qfqc

if node.right:
queue.append(node.right)
res.append(current_level_vals)
return res
```