-
Notifications
You must be signed in to change notification settings - Fork 0
Binary_Tree_Zigzag_Level_Order_Traversal #34
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?
Conversation
|
良いと思います |
| nodes_stack.append((node.right, depth + 1)) | ||
| if node.left: | ||
| nodes_stack.append((node.left, depth + 1)) | ||
| for i in range(len(zigzag_ordered)): |
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.
次のようにすると、i % 2の判定が不要になります。
for i in range(1, len(zigzag_ordered), 2):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.
なるほど、そういう選択肢はありませんでした、ありがとうございます
| next_depth_nodes.append(node.left) | ||
| if node.right: | ||
| next_depth_nodes.append(node.right) | ||
| if not left_to_right: |
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.
好みの問題かもですが、Step2の最後のもののように、resultを構築してからreverse()する方が、left_to_rightが不要になるので読みやすくなるように思います。
resultを走査するコストはありますが、多くの場合は無視できる程度かなと
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.
確かにそうですね、結構パフォーマンスを意識していまいがちですが、実務だと可読性の方が大事だと思うので留意します
fhiyo
left a comment
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.
良いと思います。reverse()を使わないバージョンも書いてみてもいいかもしれません。
| return [] | ||
| current_depth_nodes = [root] | ||
| from_left = True | ||
| next_depth_nodes = [] |
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.
消し忘れとかですかね
| same_depth_nodes.reverse() | ||
| same_depth_nodes = [] | ||
| from_left = not from_left | ||
| zigzag_ordered.append(same_depth_nodes) |
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.
same_depth_nodes が zigzag_ordered の末尾の要素の状態で、 same_depth_nodes に要素を追加していくという処理が直感的ではないように感じました。 same_depth_nodes に要素を追加したあとで、zigzag_ordered の末尾に追加したほうが、より分かりやすいコードになると思いました。
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.
ありがとうございます。
このほうが場合わけがいらず実装が簡潔になるのでこうしたのですが、確かに書いてる時にわかりにくいかもとなんとなく思ってました。
| ``` | ||
|
|
||
| - DFSでの実装 | ||
| - 反転を探索ついでにやるのはかなりややこしくなりそう(可読性も微妙になりそう)なのでやめといた |
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.
思ったほど酷いことにはならなそうです(これがいいかはおいておいて)
# 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_listsThere 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.
なるほど、ありがとうございます。(返信遅くなりました)
同じ深さのnodeを突っ込み終わるタイミングがわからず、全部突っ込んでからreverse()ができないので、突っ込む時に左からか右からか決めるしかなさそうですね(そうするとdeque使った方が良さそうですね)
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).