-
Notifications
You must be signed in to change notification settings - Fork 0
Solved Arai60/102. Binary Tree Level Order Traversal #26
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
- resultの代わりにlevel_ordered_values | ||
- https://github.com/Fuminiton/LeetCode/pull/26/files#diff-c9ad89199edb3e781ec524a54d5b5bd52c8baabcc71b3db537a3d4aeda37c9e6R58 | ||
- 下の書き方も変数が減って楽になる | ||
- 個人的には悪くないと思った |
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.
個人的にはあまり好きではないですが、長さが短ければこれでもいいのかもしれません。
def levelOrder(self, root: Optional[TreeNode]) -> List[List[int]]: | ||
if root is None: | ||
return [] | ||
nodes_and_depths: deque[Tuple[TreeNode, int]] = deque([(root, 0)]) |
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なのでdepthつかわずにlevelでもいいかもです
nodes_to_order.append(node.left) | ||
nodes_to_order.append(node.right) | ||
nodes_to_order = deque(filter(None, nodes_to_order)) | ||
return level_ordered_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.
読みやすかったです。
特に問題ないと思います。
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.
LGTM です
current_level_nodes: List[TreeNode] = [root] | ||
result: List[List[int]] = [] | ||
while current_level_nodes: | ||
node_value_list: List[int] = [] |
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.
あまり list というデータ構造であることを示す特別な意味がない (vs. stack / queue) ように思うので、単に複数形で node_values などのほうがよいかなと思いました。next_level_nodes とも align しているので。
current_level_nodes: List[TreeNode] = [root] | ||
result: List[List[int]] = [] | ||
while current_level_nodes: | ||
node_value_list: List[int] = [] |
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.
ちなみにPython3.9からは小文字の方が推奨になったようです。個人的には、LeetCodeの関数定義はそのままtyping.List
を残して、残りの部分は新しいバージョンに準拠するようにしています。
``` | ||
|
||
- フィルター関数を用いるのもあり | ||
- `nodes = list(filter(None, nodes))` |
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.
この書き方がソフトウェアエンジニアの常識に含まれているか、あまり自信がありませんでした。
X (Twitter) でアンケート取ってみます。
https://x.com/nodchip/status/1932263139762229448
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 を普段書いていないならば知らなくて当然と思いますが、
Python の Built-in function なので書くことは構わないでしょうし、別に一般論としてプロダクションのコードに未知のライブラリーが使われていることも普通ではないでしょうか。
https://source.chromium.org/search?q=%5Cbfilter%5C(%20filepath:.*%5C.py$%20-%22def%20filter%22%20case:yes&sq=
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.
念のため補足すると、 filter() 関数の第一引数に None を渡した場合、第二引数に渡された値から None を取り除いた値を返す、という挙動が、平均的なソフトウェアエンジニアにとって既知のものであるか、読みやすいかという点を気にしていました。問題なさそうということであれば、大丈夫だと思います。
node_value_list: List[int] = [] | ||
next_level_nodes: List[TreeNode] = [] | ||
for node in current_level_nodes: | ||
node_value_list.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.
リスト内包表記でresult
に直接ノードの値のリストを入れることもできます。
問題文:https://leetcode.com/problems/binary-tree-level-order-traversal/description/