Skip to content

103. Binary Tree Zigzag Level Order Traversal#27

Open
tarinaihitori wants to merge 1 commit into
mainfrom
103-binary-tree-zigzag-level-order-traversal
Open

103. Binary Tree Zigzag Level Order Traversal#27
tarinaihitori wants to merge 1 commit into
mainfrom
103-binary-tree-zigzag-level-order-traversal

Conversation

@tarinaihitori
Copy link
Copy Markdown
Owner

next_nodes = []
for node in nodes:
current_level_values.append(node.val)
if node.left:
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Noneの確認はis Noneを使うほうが私は良いと思います。
Satorien/LeetCode#2 (comment)

@@ -36,6 +36,7 @@ class Solution:

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

2ndのようにleft_to_rightTrueの時だけ特殊な処理があって、
あとはleft_to_rightによらず共通の処理が行われる構造になっている方が、使う頭が最低限でいいですよね。

current_level_values.reverse()
nodes = next_nodes
result.append(current_level_values)
level += 1
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

好みかつ大変細かい話で恐縮ですが、
148-150は、「ループで行いたい処理→次回のループのための更新」としたくなりました。

result.append(current_level_values)
level += 1
nodes = next_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.

まあ、細かいですけれども、こういうところで覚えられるかとか、毎回抜けるかとかが変わりますよね。

動かせるか確認してから、動かさない
ニンジンを切って、炊飯器のスイッチをいれて、ジャガイモを切れ、みたいな指示書かないじゃないですか。

https://discord.com/channels/1084280443945353267/1264245497040732171/1264631646775414938

def zigzagLevelOrder(self, root: Optional[TreeNode]) -> List[List[int]]:
if not root:
return []
result = []
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

resultは手抜きな命名だが、逆にzigzag_level_ordered_nodesとかもやけに長いのでresultにした。

とても共感しました。
が、やはりvalues_per_levelなどにした方が情報量があってよい気がします。

@@ -1,6 +1,6 @@
1st
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

5か月前のコミットが main に取り込まれていますね。

直したかったら、main に入っている commit を reverse するようなコミットを作り、main を README のみにすればいいんですが、ちょっとややこしいかもしれません。

とりあえず、ややこしいことをする前には一回バックアップを取るといいでしょう。

current_level_values.reverse()
nodes = next_nodes
result.append(current_level_values)
level += 1
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/1264245497040732171/1264631646775414938

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.

4 participants