Skip to content

102. Binary Tree Level Order Traversal#28

Open
xbam326 wants to merge 3 commits into
mainfrom
102
Open

102. Binary Tree Level Order Traversal#28
xbam326 wants to merge 3 commits into
mainfrom
102

Conversation

@xbam326
Copy link
Copy Markdown
Owner

@xbam326 xbam326 commented Feb 15, 2026

- その際に左から右に返す
- 幅優先探索で解けば良さそう
- 特に苦労なく書けた
- 方針として毎回level毎に新しくfrontierを作るか、levelもfrontierに入れて管理するか迷ったが毎回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.

step1の段階なのに視野が広く取れているなーと感心しました(自分は基本的にこの段階では解くので精一杯なので)。

Comment on lines +16 to +18
level_order_values = []
if root is None:
return level_order_values
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_values を空のリストとして初期化します。rootNone なら level_order_values を返します。さて戻り値はなんでしょう。」というマイクロ謎解きが発生するので、ダイレクトに空のリストを返すほうが好みです。root is None なら、そもそも level_order_values という変数の存在自体を知る必要がありません。

Suggested change
level_order_values = []
if root is None:
return level_order_values
if root is None:
return []
level_order_values = []

while current_level:
current_values = []
next_level = []
for current in current_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.

ここは現在の (current) というより、ノードであるという情報のほうが重要に思いました。

Suggested change
for current in current_level:
for node in current_level:


current_level = [root]
while current_level:
current_values = []
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

細かいですが、current_ と対応する next_values は登場せず、ライフタイムもこのループ内だけなので values でも良さそうに感じました。current_level と関係があることを示唆する current_level_values でもいいかもしれません。

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.

2 participants