Skip to content

103 binary tree zigzag level order traversal#27

Open
kitano-kazuki wants to merge 5 commits intomainfrom
103-binary-tree-zigzag-level-order-traversal
Open

103 binary tree zigzag level order traversal#27
kitano-kazuki wants to merge 5 commits intomainfrom
103-binary-tree-zigzag-level-order-traversal

Conversation

@kitano-kazuki
Copy link
Copy Markdown
Owner

* values取得のロジックが切り分けられるのは嬉しいけど、2回そのレベルを捜査する必要があるのがモヤつきはする
* 好みの問題かなぁ
* https://github.com/Satorien/LeetCode/pull/27/files
* depthを持っておいて、その偶奇を活用
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.

nanae772/leetcode-arai60#27 (comment)

個人的にはrootをはじめとして降りるごとに反転していった結果、反転するのがlevelが奇数である時に合致した、という印象を持っています。
つまりlevelの偶奇で判断するのは要件から離れていると感じるので、levelが上がるにつれてis_left_to_rightのbool値を反転させていく方が好みです。

こんな意見もあった

if node.right is not None:
next_frontier.append(node.right)
if is_right_to_left:
values_at_this_level = values_at_this_level[::-1]
Copy link
Copy Markdown
Owner Author

@kitano-kazuki kitano-kazuki Mar 24, 2026

Choose a reason for hiding this comment

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

quinn-sasha/leetcode#26 (comment)

CPython での最適化とかまでは掘ってないですが、[::-1] はコピーを作りますね。reversed の方がよいと思います。
https://stackoverflow.com/questions/37637829/difference-between-reverse-and-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.

「C++ はコピーうるさめですが、Python は細かいことをいってもしょうがないところもありますね。
いずれにせよどれくらい遅いかは定量的に考えておきましょう。」
とコメントを追加してきました。

Comment on lines +18 to +22
* 木の高さをkとした時,
* BFSは一度にリスト(あるいはキュー)に2^(k-1)個の要素が入る
* DFSはk個で済む
* メモリに2^(k-1)個のTreeNodeが乗り切らないときはDFSにした方が良い
* TODO: メモリに乗り切らないってどのくらいのバイト数からなのだろう??
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

平衡木、あるいはそれに近い構造を想定しているように見えますが、そうでない場合(例えば、一列 - 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.

メモリに乗り切らないってどのくらいのバイト数からなのだろう??

これはプログラムが実行される環境次第だと思いました。

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

パソコンのメモリのうち、空きメモリの容量くらいだと思います。実利用環境においては、スワップアウトによるスラッシングを避けるため、ある程度余裕を見ると思います。

昔話を思い出しました。

自分が本番の面接で面接官をしたときに、応募者に対して「あなたが普段使っているパソコンのメモリのサイズを教えて下さい。」と聞きました。応募者は「32 MB です。」と答えました。自分はそれを聞いて、 MB と GB を言い間違えただけだろうと思いました。自分は「32 メ・ガ・バ・イ・トですか?」と、 MB を強調して聞きました。応募者は「1 GB くらいあったかもしれません。」と答えました。私は、この応募者は、ソフトウェアエンジニアであればほとんどの人が分かっているであろう、メモリの容量に対する感覚を持っていないのだと思いました。面接終了後、私は面接レポートに悪い評価を付けました。この話を同僚にしたところ「そんな奴落としていいよ」と言われました。

一点誤解の無いよう補足すると、上記は、常識だから知っておかなければならない、ということではなく、いつの間にか知っていることなのだから常識なのだろう、という気持ちです。

return []
level_zigzag_ordered_values = []
is_right_to_left = False
frontier = deque([(root)])
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

タプルは不要そうです。

Suggested change
frontier = deque([(root)])
frontier = deque([root])

Comment on lines +88 to +91
next_frontier = deque()
values_at_this_level = []
while frontier:
node = frontier.popleft()
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

この使い方ならlistでも大丈夫そうですね。

Suggested change
next_frontier = deque()
values_at_this_level = []
while frontier:
node = frontier.popleft()
next_frontier = []
values_at_this_level = []
for node in frontier:

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.

ですね。自分には明確にpro/conがなく好みの問題かなと思っているのですがこれってどっちの方がいいとかあるのでしょうか?

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

KISS (Keep It Simple, Stupid) の法則に従うなら、よりシンプルな list を使うのが良いと思います。

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