Skip to content

103. Binary Tree Zigzag Level Order Traversal#29

Open
fhiyo wants to merge 1 commit intomainfrom
103_binary-tree-zigzag-level-order-traversal
Open

103. Binary Tree Zigzag Level Order Traversal#29
fhiyo wants to merge 1 commit intomainfrom
103_binary-tree-zigzag-level-order-traversal

Conversation

@fhiyo
Copy link
Copy Markdown
Owner

@fhiyo fhiyo commented Jun 28, 2024

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
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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を使うと副作用に頼らなくてよいので、流儀によってはこちらの方が好まれるかもしれないと思いました。

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ありがとうございます。こちらの書き方の方がhelperからそのまま値を返せるので良さそうに感じました

level_order_values.append(values)
from_left = not from_left
return level_order_values
```
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

全体的に良いと思いました。valuesという変数名、102. Binary Tree Level Order Traversalと同様もう少し説明があっても良いかと思いました。

while nodes:
values = []
next_level_nodes = []
while nodes:
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

良いと思いました!
恐らく好みの問題ですがfor ループで後ろから見ていってもよい気がしました。


### ②

右からのときはリストを反転させるパターン。難しく考えず、こういう解法を選択肢として出せるようにしておきたい。
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

これが一番読みやすい気はしますよね。意図がはっきりしています。

実務では速度よりも意図の伝わりやすさを取ることが多いです。

- https://discord.com/channels/1084280443945353267/1196472827457589338/1236595428557062214


前の問題と同様、あまりDFSで解く気にならないがやってみる。
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

これDFSになってますか?

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

なってないですねこれ。ありがとうございます。
辞書の挿入順が保存される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()]

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

実装ありがとうございました!

if not root:
return []
nodes = [root]
from_left = True
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

left_to_right という変数名でも良いと思います。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants