-
Notifications
You must be signed in to change notification settings - Fork 0
102. Binary Tree Level Order Traversal #28
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?
Conversation
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.
良いと思います!
``` | ||
思考ログ: | ||
- levelの情報を配列の要素数で管理する方法 | ||
- 一番後ろの配列に追加していけば```level```は不要 |
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.
(Step3でlevel使わないようにしていますが、ここだけ読んだ感想として)
level_ordered_values[level].append(node.val)
とすると、levelが何だったかを覚えている必要があって読み手に負担が少しかかると感じました。
level_ordered_values[-1].append(node.val)
とする方が末尾のリストに追加することが明確になるように感じます
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.
良いと思います!
思考ログ: | ||
- 時間計算量をO(N)へ | ||
- 命名が難しい | ||
- ```level_order_values```は二重リスト、```same_level_values```は単一のリストなんだけど同じ```hoge_values```というのがなんか気に入らない |
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.
pythonって変数宣言のときに型定義書くってできないんでしょうか(?? )
型宣言あれば変数名がconfusingでもまだ助かるなと思い
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.
あるにはあるんですが、関数の引数以外のところではあまり一般的ではないような印象です。
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.
最低限関数に型を書いておけば、VSCodeやPyCharm等ではそれなりに型を推論してくれて、変数名にカーソルをあてるなどすれば型が見えますが、明示的に
level_ordered_values: List[List[int]] = []
みたいな書き方もできます。mypyなどを設定すれば、型定義の漏れなどをある程度指摘させることはできます。
※ちなみに、3.9以降ではlist[list[int]]の書き方のほうが推奨になっています。
名前については、level_ordered_listsとかlevel_ordered_values_listとかもあるかもしれないですね(step3ではsame_level_valuesがなくなってますが、一応)
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.
ありがとうございます。
現状は関数の引数、返り値くらいでしか書いていないのですが、pythonのコードで型宣言をどこまで真面目にやればいいのか難しいなと。。
命名、この選択肢も考えたのですが、普段listオブジェクトの命名に_listをつけないようにしているので、うーん、となってしまいました。
if not root: | ||
return None | ||
|
||
while len(level_ordered_values) - 1 < level: |
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.
趣味の範囲でしょうが、私の趣味は
level >= len(level_ordered_value)
です。
level < len(level_ordered_value)
ではない時という主張に見えるので。
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.
ありがとうございます。
ご提案頂いた表現の方が良いように思いました。
特に注目しているlevel
は左辺に置きたい気がします。
他の方がコメントしてるところ以外は良いと思います |
https://leetcode.com/problems/binary-tree-level-order-traversal/description/