103. Binary Tree Zigzag Level Order Traversal#27
Open
TakayaShirai wants to merge 1 commit intomainfrom
Open
Conversation
mamo3gr
reviewed
Feb 18, 2026
Comment on lines
+19
to
+38
| // あとは何があるかな。 | ||
| // 値の格納と、次の層の格納をしなければならない。 | ||
| // 分けて2回でやる方法がありそう。 | ||
| // 箱の中でノードが一列に並んでいるとすれば、 | ||
| // 値を格納する処理は、 | ||
| // - 左から右で箱に格納されているノードを、そのままの前からの順で値を格納していく。 | ||
| // - 右から左で箱に格納されているノードも、そのままの前からの順で値を格納していく。 | ||
| // 次の層を格納する処理としては、 | ||
| // - 左から右で箱に格納されているノードから、次の層のノードを右から左に格納する | ||
| // - 右から左に箱に格納されているノードから、次の層のノードを左から右に格納する | ||
| // つまり、 | ||
| // - 左から右の順に格納されている場合は、最後尾から右、左の順に子ノードを格納する | ||
| // - 右から左の順に格納されている場合は、最後尾から左、右の順に子ノードを格納する。 | ||
| // これを考えると、 | ||
| // 値の格納と、次の層の格納の方向が逆で一度にどちらもやるのはむずかしそうに思える。だから、2回やりたい。 | ||
| // もっといい方法があるかもしれないが、とりあえずこれでやってみる。計算量は O(N) で、2回走査が入るからそこの定数倍は入る。 | ||
| // 処理としては、1回目の走査で、値の格納。2回目の走査で、子ノードの格納を2回。 | ||
| // 結局走査が1回分増えるだけだから、3N か 4N の違いな気がする。だとするなら、N <= 2000 で Dart は 10^7 steps/s だとすると、 | ||
| // 2000 / 10^7 = 2 * 10^-4 = 0.2 ms しか変わらないから、全く問題ない。後で合っているか確かめる。 | ||
| // だいぶパズル的な要素があり、これを採用するのも、可読性的に微妙な気がする。 |
|
特に違和感ありませんでした。実装パターンを幅広く試していて良かったです。 |
dxxsxsxkx
reviewed
Feb 18, 2026
| } | ||
|
|
||
| // 計測時間 | ||
| // リスト生成には、時間がかかるらしい。インデックスの方法がやはり一番早いが、たいした違いはない。 |
There was a problem hiding this comment.
複数のやり方の所要時間を都度計算する発想がなかったので、勉強になります。
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
解いた問題:https://leetcode.com/problems/binary-tree-zigzag-level-order-traversal/description/
次に解く問題:https://leetcode.com/problems/validate-binary-search-tree/description/