Open
Conversation
Comment on lines
+124
to
+128
| level_order_values.append(values) | ||
| zigzag_level_order_helper(next_level_nodes, not from_left) | ||
|
|
||
| zigzag_level_order_helper([root], True) | ||
| return level_order_values |
There was a problem hiding this comment.
Suggested change
| level_order_values.append(values) | |
| zigzag_level_order_helper(next_level_nodes, not from_left) | |
| zigzag_level_order_helper([root], True) | |
| return level_order_values | |
| yield values | |
| yield from zigzag_level_order_helper(next_level_nodes, not from_left) | |
| return list(zigzag_level_order_helper([root], True)) |
yieldを使うと副作用に頼らなくてよいので、流儀によってはこちらの方が好まれるかもしれないと思いました。
Owner
Author
There was a problem hiding this comment.
ありがとうございます。こちらの書き方の方がhelperからそのまま値を返せるので良さそうに感じました
Mike0121
reviewed
Jun 29, 2024
| level_order_values.append(values) | ||
| from_left = not from_left | ||
| return level_order_values | ||
| ``` |
There was a problem hiding this comment.
全体的に良いと思いました。valuesという変数名、102. Binary Tree Level Order Traversalと同様もう少し説明があっても良いかと思いました。
sakupan102
reviewed
Jun 29, 2024
| while nodes: | ||
| values = [] | ||
| next_level_nodes = [] | ||
| while nodes: |
There was a problem hiding this comment.
良いと思いました!
恐らく好みの問題ですがfor ループで後ろから見ていってもよい気がしました。
oda
reviewed
Jun 30, 2024
|
|
||
| ### ② | ||
|
|
||
| 右からのときはリストを反転させるパターン。難しく考えず、こういう解法を選択肢として出せるようにしておきたい。 |
There was a problem hiding this comment.
これが一番読みやすい気はしますよね。意図がはっきりしています。
実務では速度よりも意図の伝わりやすさを取ることが多いです。
TORUS0818
reviewed
Jul 30, 2024
| - https://discord.com/channels/1084280443945353267/1196472827457589338/1236595428557062214 | ||
|
|
||
|
|
||
| 前の問題と同様、あまりDFSで解く気にならないがやってみる。 |
Owner
Author
There was a problem hiding this comment.
なってないですねこれ。ありがとうございます。
辞書の挿入順が保存されるPython3.7以降の環境を前提に書くならこんな感じですかね。
class Solution:
def zigzagLevelOrder(self, root: Optional[TreeNode]) -> List[List[int]]:
if not root:
return []
level_to_values = defaultdict(deque)
stack = [(root, 0)]
while stack:
node, level = stack.pop()
if level % 2 == 0:
level_to_values[level].append(node.val)
else:
level_to_values[level].appendleft(node.val)
# To visit the left node first, we push the right node to the stack first.
if node.right:
stack.append((node.right, level + 1))
if node.left:
stack.append((node.left, level + 1))
# We don't need to sort level_to_values.items(),
# because the key-value pairs of level_to_values must be added in ascending order by level.
return [list(values) for values in level_to_values.values()]
nodchip
reviewed
Jul 30, 2024
| if not root: | ||
| return [] | ||
| nodes = [root] | ||
| from_left = True |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
https://leetcode.com/problems/binary-tree-zigzag-level-order-traversal/description/