-
Notifications
You must be signed in to change notification settings - Fork 0
Create binary-tree-level-order-traversal.md #16
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: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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 = [[]] | ||
| while queue: | ||
| node, depth = queue.pop(0) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. list の pop(0) は計算量が長さ分かかるので、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回目も同様。 | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. あ、Gemini に聞くのいいと思います。
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 重ね重ねすみません。以下の3人のを読むようにします。 |
||
|
|
||
| ```py | ||
| from collections import deque | ||
| class Solution: | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. class の前には2つ空行を入れるのが一般的と思われます。 |
||
| 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) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. この書き方の場合、for ループの途中では今のレベルのノードと次のレベルのノードが存在する点が懸念点としてあるかなと思いました。 |
||
| if node.right: | ||
| queue.append(node.right) | ||
| res.append(current_level_vals) | ||
| return res | ||
| ``` | ||
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.
変数名が省略した形だと読み手が考えることが増えるので省略しない方が好まれます。
https://google.github.io/styleguide/pyguide.html#316-naming
個人的には result という変数名はこの規模の関数ならOKだと思っています。
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.
コメントありがとうございます!手癖で使ってました...!