Skip to content

102. Binary Tree Level Order Traversal#40

Open
skypenguins wants to merge 1 commit into
mainfrom
leetcode/arai60/problem-102
Open

102. Binary Tree Level Order Traversal#40
skypenguins wants to merge 1 commit into
mainfrom
leetcode/arai60/problem-102

Conversation

@skypenguins

Copy link
Copy Markdown
Owner

102. Binary Tree Level Order Traversal

次回予告: 103. Binary Tree Zigzag Level Order Traversal

@skypenguins skypenguins self-assigned this Jun 28, 2026
Comment thread memo.md
Comment on lines +174 to +177
if node.left:
pending_nodes.append(node.left)
if node.right:
pending_nodes.append(node.right)

@Hiroto-Iizuka Hiroto-Iizuka Jun 28, 2026

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 node is not Noneを使う方がよさそうです。

ksaito0629/leetcode_arai60#1 (comment)

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.

確かに if node is not None の方が安全ですね。

Comment thread memo.md
Comment on lines +161 to +164
if root is None:
return []

level_order = []

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

level_orderを一番最初に定義しておけば、root is None の返り値としても使用可能になると思いました。

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.

確かにおっしゃる通りですね。

読みやすさとしては議論の余地がありますが、以下のようにするとガード節自体をなくせることに気がつきました。

pending_nodes = deque([root] if root else [])

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

個人的には、 early return を先に書いたほうが、読み手に取って読みやすくなると思います。理由は、関数の宣言と early return のあいだにコードが挟まっていると、そのコードの内容を early return のコードを読むあいだ、短期記憶にとどめておかなければならないためです。
また、level_order を最初に定義し、 root is None の返り値として使用した場合、 level_order の定義を思い返さなければなりません。これは、小規模ではありますが、ややパズルに感じます。

Comment thread memo.md
if root is None:
return []

level_by_level = []

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

level_values values_at_level といった変数名も考えられそうです。このあたり、自分には英語のニュアンスの違いが分かりませんでした。申し訳ありません。

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.

この命名 level_by_level に関しては、「レベルの中にレベルがある」を意図しました。意図が伝わらなさそうでしたのでStep3では改善しました。

Comment thread memo.md
Comment on lines +161 to +164
if root is None:
return []

level_order = []

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

個人的には、 early return を先に書いたほうが、読み手に取って読みやすくなると思います。理由は、関数の宣言と early return のあいだにコードが挟まっていると、そのコードの内容を early return のコードを読むあいだ、短期記憶にとどめておかなければならないためです。
また、level_order を最初に定義し、 root is None の返り値として使用した場合、 level_order の定義を思い返さなければなりません。これは、小規模ではありますが、ややパズルに感じます。

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

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants