Skip to content

Conversation

@nittoco
Copy link
Owner

@nittoco nittoco commented Aug 24, 2024

https://leetcode.com/problems/binary-tree-zigzag-level-order-traversal/description/

Given the root of a binary tree, return the zigzag level order traversal of its nodes' values. (i.e., from left to right, then right to left for the next level and alternate between).

@Yoshiki-Iwasa
Copy link

良いと思います

nodes_stack.append((node.right, depth + 1))
if node.left:
nodes_stack.append((node.left, depth + 1))
for i in range(len(zigzag_ordered)):

Choose a reason for hiding this comment

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

次のようにすると、i % 2の判定が不要になります。

        for i in range(1, len(zigzag_ordered), 2):

Copy link
Owner Author

Choose a reason for hiding this comment

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

なるほど、そういう選択肢はありませんでした、ありがとうございます

next_depth_nodes.append(node.left)
if node.right:
next_depth_nodes.append(node.right)
if not left_to_right:

Choose a reason for hiding this comment

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

好みの問題かもですが、Step2の最後のもののように、resultを構築してからreverse()する方が、left_to_rightが不要になるので読みやすくなるように思います。
resultを走査するコストはありますが、多くの場合は無視できる程度かなと

Copy link
Owner Author

Choose a reason for hiding this comment

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

確かにそうですね、結構パフォーマンスを意識していまいがちですが、実務だと可読性の方が大事だと思うので留意します

Copy link

@fhiyo fhiyo left a comment

Choose a reason for hiding this comment

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

良いと思います。reverse()を使わないバージョンも書いてみてもいいかもしれません。

return []
current_depth_nodes = [root]
from_left = True
next_depth_nodes = []
Copy link

Choose a reason for hiding this comment

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

消し忘れとかですかね

same_depth_nodes.reverse()
same_depth_nodes = []
from_left = not from_left
zigzag_ordered.append(same_depth_nodes)
Copy link

Choose a reason for hiding this comment

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

same_depth_nodeszigzag_ordered の末尾の要素の状態で、 same_depth_nodes に要素を追加していくという処理が直感的ではないように感じました。 same_depth_nodes に要素を追加したあとで、zigzag_ordered の末尾に追加したほうが、より分かりやすいコードになると思いました。

Copy link
Owner Author

Choose a reason for hiding this comment

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

ありがとうございます。
このほうが場合わけがいらず実装が簡潔になるのでこうしたのですが、確かに書いてる時にわかりにくいかもとなんとなく思ってました。

```

- DFSでの実装
- 反転を探索ついでにやるのはかなりややこしくなりそう(可読性も微妙になりそう)なのでやめといた

Choose a reason for hiding this comment

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

思ったほど酷いことにはならなそうです(これがいいかはおいておいて)

# Definition for a binary tree node.
# class TreeNode:
#     def __init__(self, val=0, left=None, right=None):
#         self.val = val
#         self.left = left
#         self.right = right
class Solution:
    def zigzagLevelOrder(self, root: Optional[TreeNode]) -> List[List[int]]:
        if not root:
            return []

        node_level_pairs = [(root, 0)]
        zigzag_ordered_lists = []
        while node_level_pairs:
            node, level = node_level_pairs.pop()
            while level >= len(zigzag_ordered_lists):
                zigzag_ordered_lists.append([])

            if level % 2:
                zigzag_ordered_lists[level].insert(0, node.val)
            else:
                zigzag_ordered_lists[level].append(node.val)

            if node.right: node_level_pairs.append((node.right, level + 1))
            if node.left: node_level_pairs.append((node.left, level + 1))
    
        return zigzag_ordered_lists

Copy link
Owner Author

Choose a reason for hiding this comment

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

なるほど、ありがとうございます。(返信遅くなりました)
同じ深さのnodeを突っ込み終わるタイミングがわからず、全部突っ込んでからreverse()ができないので、突っ込む時に左からか右からか決めるしかなさそうですね(そうするとdeque使った方が良さそうですね)

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