Conversation
| return [] | ||
|
|
||
| queue = [(root, 0)] | ||
| res = [[]] |
There was a problem hiding this comment.
変数名が省略した形だと読み手が考えることが増えるので省略しない方が好まれます。
https://google.github.io/styleguide/pyguide.html#316-naming
個人的には result という変数名はこの規模の関数ならOKだと思っています。
There was a problem hiding this comment.
コメントありがとうございます!手癖で使ってました...!
| queue = [(root, 0)] | ||
| res = [[]] | ||
| while queue: | ||
| node, depth = queue.pop(0) |
There was a problem hiding this comment.
list の pop(0) は計算量が長さ分かかるので、collections.deque を使うのが良いと思います。
https://docs.python.org/3/library/collections.html#collections.deque
| node = queue.popleft() | ||
| current_level_vals.append(node.val) | ||
| if node.left: | ||
| queue.append(node.left) |
There was a problem hiding this comment.
この書き方の場合、for ループの途中では今のレベルのノードと次のレベルのノードが存在する点が懸念点としてあるかなと思いました。
先に解いた人のコードやコメント集をみると色々な書き方の幅があって勉強になると感じています。
見たコードがあればそのリンクと思ったことを書いていただくとコメントがもらいやすくなると思います。例えばこの問題だと queue でなく、list を二つ(今のループで見るべきノード、次のループでみるべきノード)使って書くこともできますよね。
https://docs.google.com/document/d/11HV35ADPo9QxJOpJQ24FcZvtvioli770WWdZZDaLOfg/edit?tab=t.0#heading=h.gp4hkfr3qfqc
|
|
||
| ```py | ||
| from collections import deque | ||
| class Solution: |
There was a problem hiding this comment.
class の前には2つ空行を入れるのが一般的と思われます。
https://google.github.io/styleguide/pyguide.html#35-blank-lines
スタイルの問題なので周囲の人と合わせるのが無難です。
| ``` | ||
|
|
||
| 2回目。Gemini に聞いて、level ごとに for ループを回したほうが level order traversal としては標準的だし直観的と指摘される。 | ||
| 3回目も同様。 |
There was a problem hiding this comment.
あ、Gemini に聞くのいいと思います。
あと、読むのをできればやって欲しくて、たとえば、完走した人の中で気に入った人を見つけておくといいと思います。
There was a problem hiding this comment.
重ね重ねすみません。以下の3人のを読むようにします。
https://leetcode.com/problems/binary-tree-level-order-traversal/