Skip to content
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

Add 102. Binary Tree Level Order Traversal.md #26

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open

Add 102. Binary Tree Level Order Traversal.md #26

wants to merge 1 commit into from

Conversation

t0hsumi
Copy link
Owner

@t0hsumi t0hsumi commented Mar 4, 2025

```

recursive, preorder DFS traversal,
2パターン書いてみたが、あまり名前がしっくりこない。
Copy link

Choose a reason for hiding this comment

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

全体的に私は不満ないです。大規模言語モデルがいい案くれたりします。

result = []
while nodes:
values_in_this_level = []
nodes_in_next_level = []
Copy link

Choose a reason for hiding this comment

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

好みの問題ですが変数名の中に前置詞が入ると変数名が長くなりがちなので、それを避けるためにnext_level_nodesみたいにすることもあります

return

nonlocal level_ordered_values
while not len(level_ordered_values) > level:
Copy link

@colorbox colorbox Mar 6, 2025

Choose a reason for hiding this comment

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

条件部分にnotがあると読む時に反転させる一手間で若干読みづらくなるので、notをつけると読みやすくなる状況以外はnotをつけないことをおすすめします。

Copy link
Owner Author

Choose a reason for hiding this comment

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

確かに上から下に読むものと考えたらここの否定は分かりにくいですね。

自分の中では、このループを抜けたつぎの行のlevel_ordered_values[level].append(node.val)で例外を発生させない条件の否定みたいな意味で書いてました。
つまり、ループを抜ければ、level_ordered_values[level].append(node.val)はうまく通るだろうみたいなニュアンスです。

return

nonlocal level_ordered_values
while not len(level_ordered_values) > level:
Copy link

@colorbox colorbox Mar 6, 2025

Choose a reason for hiding this comment

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

ここwhileである必然性がなさそうで、なぜwhileなんだろうという違和感を感じます。

Copy link
Owner Author

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/1200089668901937312/1211248049884499988
これを参考にしてます。
preorder traversalをしており一段飛ばしになるとかはないので、ifでもいい場所ではあります。
必然性とまで言えるような論拠はないです。

return level_ordered_values
while not len(level_ordered_values) > level:
level_ordered_values.append([])
level_ordered_values[level].append(node.val)
Copy link

Choose a reason for hiding this comment

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

この書き方「階層化していないBFS」でも使っていますが、便利ですね。

Comment on lines +20 to +22
if node.left is not None:
nodes_in_next_level.append(node.left)
if node.right is not None:

Choose a reason for hiding this comment

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

is not None はなくてもシンプルで良いですが、書いた方が明示的という人もいるかもしれません。
好みでしょうか。

nodes = [root]
result = []
while True:
nodes = list(filter(None, nodes))

Choose a reason for hiding this comment

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

取り出す前にfilterで除去するというのは、今まで考えたことがありませんでしたが、振る舞いとしては理にかなった操作だと感じました。

- https://github.com/hayashi-ay/leetcode/pull/32/files
- DFS preorder recursive traversal、階層別でないdequeを使ったBFS

感想:BFS以外の書き方が思いつかなかったが、levelさえトラックしていればDFSでやろうが動作する。
Copy link

@olsen-blue olsen-blue Mar 8, 2025

Choose a reason for hiding this comment

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

DFSは直感に反する感じがして食わず嫌いしてましたが、解けるんですね。勉強になりました。

それと、深さごとに、入る部屋[]が決まってて、そこに放り込んでいく操作ですが、辞書みたいな使い方で良いですね。
level_ordered_values[level].append(node.val)

nodes_in_next_level.append(node.right)
nodes_in_level = nodes_in_next_level
level_ordered_values.append(values_in_level)
return level_ordered_values
Copy link

Choose a reason for hiding this comment

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

好みなのですが、変数名が全体的に長く感じました。

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.

6 participants