-
Notifications
You must be signed in to change notification settings - Fork 0
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?
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,28 @@ | ||
| # 102. Binary Tree Level Order Traversal | ||
|
|
||
| https://leetcode.com/problems/binary-tree-level-order-traversal/ | ||
|
|
||
| ## step1(まず通す) | ||
|
|
||
| 幅優先探索をする時に階層ごとにループを回せばいい。 | ||
|
|
||
| ノード数 n に対して時間計算量 O(n)、空間計算量 O(n) | ||
|
|
||
| ノード数の上限は 2000 であり各ノードについて val の取得、子の None 判定とリストへの append などを行う程度なので、計算回数は `2000 * 10 = 2 * 10 ^ 4`くらい、計算時間は 数十 ms くらいか。 | ||
|
|
||
| ## step2(整形&他の人のコードを読む) | ||
|
|
||
| - https://github.com/shintaro1993/arai60/pull/30/changes | ||
| - この方もそうだし、問題タイトルや文でも level という語が使われている(し、level のほうがよく聞く気がする)ので layer -> level に変数名を変更 | ||
| - https://github.com/naoto-iwase/leetcode/pull/30/changes#diff-40c0807c5c71abb50c5c8ebe78ffc21551f9ab6d80ccd6a156c960d1febd1882R19 | ||
| - 「キューにはNoneでないことを確かめていれる方が経験で見通しよく書けるので、そうする。」 | ||
| - 見通しの他に、None を探索詰めないことで探索回数が半分程度に減るというメリットもありそう。 | ||
| - https://github.com/naoto-iwase/leetcode/pull/30/changes#diff-40c0807c5c71abb50c5c8ebe78ffc21551f9ab6d80ccd6a156c960d1febd1882R20 | ||
| - 「計算量 Time: O(V + E) = O(V), Space: O(width) = O(V)」 | ||
| - 自分では全部のノードを見るから O(V)、と雑に考えていたが、実際には高さの回数ループが回り、その中で同じ高さのノードの数だけループが回るという構造であることに気をつける必要がある | ||
|
Comment on lines
+21
to
+22
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. 自分もわりとO(V)と大雑把に考えがちなので,勉強になりました. |
||
| - https://github.com/tom4649/Coding/pull/25/changes | ||
| - 変数名 traversal はちょっと変な気がするので、この方が使っている `values_by_level` とか他の方が書いていたような `level_by_level` がいいかもしれない | ||
|
|
||
| ## step3(10分以内にさっとかける \* 3回) | ||
|
|
||
| https://github.com/MA-yo-TA/leetcode/pull/22/changes#diff-dce60424cb6c268dbcb886bdef92379cfa43ce43bf656d9733daae9cc937f12dR71 で書いてあることと逆をやっているが、今回は values があるため、next_layer も含めてループ内の一時変数という感覚になった。まだ自分の好みが定まってないように感じる。 | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| from typing import Optional | ||
|
|
||
|
|
||
| class TreeNode: | ||
| def __init__(self, val=0, left=None, right=None): | ||
| self.val = val | ||
| self.left = left | ||
| self.right = right | ||
|
|
||
|
|
||
| class Solution: | ||
| def levelOrder(self, root: Optional[TreeNode]) -> list[list[int]]: | ||
| if root is None: | ||
| return [] | ||
|
|
||
| nodes: list[TreeNode] = [root] | ||
| traversal: list[list[int]] = [] | ||
|
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.
|
||
| while nodes: | ||
| values: list[int] = [] | ||
| next_layer: list[TreeNode] = [] | ||
| for node in nodes: | ||
| values.append(node.val) | ||
| if node.left is not None: | ||
| next_layer.append(node.left) | ||
| if node.right is not None: | ||
| next_layer.append(node.right) | ||
|
|
||
| traversal.append(values) | ||
| nodes = next_layer | ||
|
|
||
| return traversal | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| from typing import Optional | ||
|
|
||
|
|
||
| class TreeNode: | ||
| def __init__(self, val=0, left=None, right=None): | ||
| self.val = val | ||
| self.left = left | ||
| self.right = right | ||
|
|
||
|
|
||
| class Solution: | ||
| def levelOrder(self, root: Optional[TreeNode]) -> list[list[int]]: | ||
| if root is None: | ||
| return [] | ||
|
|
||
| nodes: list[TreeNode] = [root] | ||
| values_by_level: list[list[int]] = [] | ||
| while nodes: | ||
| values: list[int] = [] | ||
| next_level: list[TreeNode] = [] | ||
| for node in nodes: | ||
| values.append(node.val) | ||
| if node.left is not None: | ||
| next_level.append(node.left) | ||
| if node.right is not None: | ||
| next_level.append(node.right) | ||
|
|
||
| values_by_level.append(values) | ||
| nodes = next_level | ||
|
|
||
| return values_by_level |
|
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. 読みやすかったです |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| from typing import Optional | ||
|
|
||
|
|
||
| class TreeNode: | ||
| def __init__(self, val=0, left=None, right=None): | ||
| self.val = val | ||
| self.left = left | ||
| self.right = right | ||
|
|
||
|
|
||
| class Solution: | ||
| def levelOrder(self, root: Optional[TreeNode]) -> list[list[int]]: | ||
| if root is None: | ||
| return [] | ||
|
|
||
| nodes = [root] | ||
|
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. 自分なら命名として
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. ありがとうございます。どういうノードかを明確に表現するのもわかりやすいですね。 |
||
| values_by_level = [] | ||
| while nodes: | ||
| values = [] | ||
| next_layer = [] | ||
| for node in nodes: | ||
| values.append(node.val) | ||
| if node.left is not None: | ||
| next_layer.append(node.left) | ||
| if node.right is not None: | ||
| next_layer.append(node.right) | ||
|
|
||
| values_by_level.append(values) | ||
| nodes = next_layer | ||
|
|
||
| return values_by_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.
👍