-
Notifications
You must be signed in to change notification settings - Fork 0
103. Binary Tree Zigzag Level Order Traversal #29
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
| node = nodes.pop() | ||
| if from_right: | ||
| zigzag_level_ordered_values[-1].append(node.val) | ||
| append_node_if_exist(node.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.
一見して何にノードを入れるのかわからないと思いました。
ノードを入れる対象のオブジェクトも引数にしていい気がします。
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.
はい、どうかなと思ったんですが、やはり突っ込まれましたね。
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.
確かに重複多いですが、中身が2行だと関数にするか難しいところですよね。
| zigzag_level_ordered_values[level] \ | ||
| = zigzag_level_ordered_values[level] + [node.val] |
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.
イコールの前で改行する&イコールの位置が前の行の頭と同じなのあまり見ない気がしました。書くならこっち↓の方が自然かも?
zigzag_level_ordered_values[level] = \
zigzag_level_ordered_values[level] + [node.val]あとelse側と書き方を揃えたのかなと思いつつ、無駄にコピーが走るのでこちらは
zigzag_level_ordered_values[level].append(node.val)でいい気がしました。
| return [] | ||
|
|
||
| zigzag_level_ordered_values = [] | ||
| def zigzag_level_order_helper(nodes: list[Optional[TreeNode]], level: int) -> None: |
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.
nodesの型、 list[TreeNode] じゃないですか?nodesの要素にNoneが入るケースは無い気がします
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.
そうですね。
深く考えずコピペしてしまいました。。
| num_same_level_nodes = len(nodes) | ||
| for _ in range(num_same_level_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.
num_same_level_nodesは作る必要ない気がします。 for _ in range(len(nodes)) で良いと思いました。さらに、 for node in reversed(nodes) でpopしないか while nodes でpopするで良い気がしました。
| zigzag_level_ordered_values[level] \ | ||
| = [node.val] + zigzag_level_ordered_values[level] |
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.
zigzag_level_ordered_values[level].insert(0, node.val) の方がコピーを生成しない分効率が良いと思います。
| while level >= len(zigzag_level_ordered_values): | ||
| zigzag_level_ordered_values.append([]) |
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.
レベル順に見ていっているので zigzag_level_ordered_values.append([]) だけでも個人的にはパズル感はありませんが、どうでしょうか。
| if from_right: | ||
| values.reverse() | ||
|
|
||
| zigzag_level_ordered_values.append(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.
こちらのように、レベルごとに値を集めて最後に答えに追加する書き方の方が読みやすかったです。
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.
FBありがとうございます!
https://leetcode.com/problems/binary-tree-zigzag-level-order-traversal/description/