-
Notifications
You must be signed in to change notification settings - Fork 0
Create 102. Binary Tree Level Order Traversal.md #7
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
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,116 @@ | ||||||||
| 15分で1回目は解けず、どのようにして同じ階層のノードを取り出して処理し、次の階層に移動するかが整理できませんでした。 | ||||||||
| 各階層から値を列挙した後に、その回数分for文を回すのは、BFSの基本ができていないから思いつかなかった(考えられなかった)のかなと思います。 | ||||||||
|
|
||||||||
| ### 1回目 | ||||||||
| 時間計算量: O(N) | ||||||||
| 空間計算量: O(N + 正解分) | ||||||||
| ```python | ||||||||
| class Solution: | ||||||||
| def levelOrder(self, root: Optional[TreeNode]) -> List[List[int]]: | ||||||||
| if not root: | ||||||||
| return [] | ||||||||
|
|
||||||||
| result = [] | ||||||||
| next_level_nodes = deque() | ||||||||
| next_level_nodes.append(root) | ||||||||
|
|
||||||||
| while next_level_nodes: | ||||||||
| same_level_nodes = [] | ||||||||
| for i in range(len(next_level_nodes)): | ||||||||
| node = next_level_nodes.popleft() | ||||||||
| same_level_nodes.append(node.val) | ||||||||
|
|
||||||||
| if node.left: | ||||||||
| next_level_nodes.append(node.left) | ||||||||
|
|
||||||||
| if node.right: | ||||||||
| next_level_nodes.append(node.right) | ||||||||
|
|
||||||||
| result.append(same_level_nodes) | ||||||||
|
|
||||||||
| return result | ||||||||
| ``` | ||||||||
|
|
||||||||
|
|
||||||||
| 理屈が頭の中にある程度整理されていれば、余計な改行が減る気がしました。 | ||||||||
|
|
||||||||
| ### 2回目 | ||||||||
| ```python | ||||||||
| class Solution: | ||||||||
| def levelOrder(self, root: Optional[TreeNode]) -> List[List[int]]: | ||||||||
| if not root: | ||||||||
| return [] | ||||||||
|
|
||||||||
| result = [] | ||||||||
| next_level_nodes = deque() | ||||||||
| next_level_nodes.append(root) | ||||||||
|
|
||||||||
| while next_level_nodes: | ||||||||
| same_level_nodes = [] | ||||||||
| for _ in range(len(next_level_nodes)): | ||||||||
| node = next_level_nodes.popleft() | ||||||||
| same_level_nodes.append(node.val) | ||||||||
| if node.left: | ||||||||
| next_level_nodes.append(node.left) | ||||||||
| if node.right: | ||||||||
| next_level_nodes.append(node.right) | ||||||||
|
|
||||||||
| result.append(same_level_nodes) | ||||||||
| return result | ||||||||
| ``` | ||||||||
|
|
||||||||
| next_level_nodes, same_level_valuesは、next_level, same_levelでも良いかと思った。 | ||||||||
|
|
||||||||
| ### 3回目 | ||||||||
| ```python | ||||||||
| class Solution: | ||||||||
| def levelOrder(self, root: Optional[TreeNode]) -> List[List[int]]: | ||||||||
| if not root: | ||||||||
| return [] | ||||||||
|
|
||||||||
| result = [] | ||||||||
|
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. resultはもう少し命名頑張っても良いかも。
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. ありがとうございます。 |
||||||||
| next_level_nodes = deque() | ||||||||
| next_level_nodes.append(root) | ||||||||
|
Comment on lines
+72
to
+73
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. こうも書けますね。
Suggested change
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. 存じ上げなかったです、ありがとうございます。 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. ドキュメントに軽く目を通して置くと良いと思います。
https://docs.python.org/3/library/collections.html#deque-objects |
||||||||
|
|
||||||||
| while next_level_nodes: | ||||||||
| same_level_values = [] | ||||||||
|
|
||||||||
| for _ in range(len(next_level_nodes)): | ||||||||
| node = next_level_nodes.popleft() | ||||||||
| same_level_values.append(node.val) | ||||||||
| if node.left: | ||||||||
| next_level_nodes.append(node.left) | ||||||||
| if node.right: | ||||||||
| next_level_nodes.append(node.right) | ||||||||
| result.append(same_level_values) | ||||||||
|
|
||||||||
| return result | ||||||||
| ``` | ||||||||
|
|
||||||||
|
|
||||||||
| ### 4回目 (listによる解答) | ||||||||
| ```python | ||||||||
| class Solution: | ||||||||
| def levelOrder(self, root: Optional[TreeNode]) -> List[List[int]]: | ||||||||
| if not root: | ||||||||
| return [] | ||||||||
|
|
||||||||
| level_ordered_values = [] | ||||||||
| current_level_nodes = [root] | ||||||||
|
|
||||||||
| while current_level_nodes: | ||||||||
| same_level_values = [] | ||||||||
|
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. same_levelとcurrent_levelの違いは何ですか?
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. same_level: 同じ階層(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. current_levelで統一して良さそうな気がします。
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. たしかに、分けて変数名つけるほどのメリットは感じず、nodeとvalueで分かれていれば十分ですね。 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. 私はそもそもローカル変数に current を付けておけばいい名前になっているというのが相当違和感です。current は多くの場合、context のようなグローバルな設定などに用いられる傾向が強く、今注目しているもの、というつもりだと標準的用法からかなりずれを感じます。変数名は長ければ読みやすい訳ではないんです。previous, next との対比ならばまだしも。 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. odaさん、liquo-riceさん |
||||||||
| next_level_nodes = [] | ||||||||
|
|
||||||||
| for i in range(len(current_level_nodes)): | ||||||||
|
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. ありがとうございます、こちらは修正必須ですね。 |
||||||||
| node = current_level_nodes[i] | ||||||||
| same_level_values.append(node.val) | ||||||||
| if node.left: | ||||||||
| next_level_nodes.append(node.left) | ||||||||
| if node.right: | ||||||||
| next_level_nodes.append(node.right) | ||||||||
| current_level_nodes = next_level_nodes | ||||||||
| level_ordered_values.append(same_level_values) | ||||||||
|
|
||||||||
| 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.
popleft()すると n = len(next_level_nodes) として、時間計算量 O(n) かかってしまいます。理由は、 popleft() した場合、右側の要素を全て 1 つずつ左にずらさなければならないためです。 (CPython の実装がそのようになっているかは確認しておりません。念のため確認をお願いいたします。)node の list を 2 つ持ち、片方を for 文で回し、もう片方に子ノードを入れていき、 1 レベル終わったら後者を全車に代入する、という実装はいかがでしょうか?
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.
dequeなのでpopleftの計算量はO(1)ではないですか?
https://docs.python.org/3/library/collections.html#collections.deque
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 を使っていると見間違えました。失礼いたしました。
Uh oh!
There was an error while loading. Please reload this page.
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.
nodchipさん、ahayashiさん
ありがとうございます。listを使って、popleftなしでも書き直してみます。
実装を確認してみました。
https://github.com/python/cpython/blob/main/Modules/_collectionsmodule.c
サイズやindexのインクリメントで、O(1)と思います。もし、参照先や読み方間違っていたらぜひ教えて欲しいです。
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 で popleft() をしているのだと勘違いしました。失礼いたしました。
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.
いえ、アドバイスと確認の機会、ありがとうございました。
参考になりました。