Skip to content

Create binary-tree-level-order-traversal.md#16

Open
tshimosake wants to merge 1 commit intomasterfrom
tshimosake-patch-14
Open

Create binary-tree-level-order-traversal.md#16
tshimosake wants to merge 1 commit intomasterfrom
tshimosake-patch-14

Conversation

@tshimosake
Copy link
Owner

return []

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

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
Owner Author

Choose a reason for hiding this comment

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

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

queue = [(root, 0)]
res = [[]]
while queue:
node, depth = queue.pop(0)

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

node = queue.popleft()
current_level_vals.append(node.val)
if node.left:
queue.append(node.left)

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


```py
from collections import deque
class Solution:

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
スタイルの問題なので周囲の人と合わせるのが無難です。

```

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

Choose a reason for hiding this comment

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

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

Copy link
Owner Author

Choose a reason for hiding this comment

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

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants