Skip to content

103. Binary Tree Zigzag Level Order Traversal - #27

Open
hiroki-horiguchi-dev wants to merge 1 commit into
mainfrom
tree-bst-103
Open

103. Binary Tree Zigzag Level Order Traversal#27
hiroki-horiguchi-dev wants to merge 1 commit into
mainfrom
tree-bst-103

Conversation

@hiroki-horiguchi-dev

Copy link
Copy Markdown
Owner

@hiroki-horiguchi-dev hiroki-horiguchi-dev self-assigned this Jun 7, 2026
Comment thread tree-bst/103.md
}

Deque<TreeNode> nodes = new ArrayDeque<>();
boolean isLeftToRight = true;

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

識別子が動詞の原形や命令形から始まっている場合、関数名のように感じる場合があります。自分なら leftToRight と名付けると思います。

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.

コメントありがとうございます。
あれ、本当ですか・・・
is~ を、「その状態かどうか?」を示すフラグ用の変数名として、今まで参画したプロダクトの本番コードでも多々使ってきてしまいました。

Comment thread tree-bst/103.md
nodes.addLast(root);

while (!nodes.isEmpty()) {
// 本当は List<Integer> で受けたいが、jdk21未満だと List に addLast, addFirst がないため、コンパイルエラーになる可能性があるので、LinkedList で受けている。

@nodchip nodchip Jun 8, 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.

LinkedList は、 add 毎に新しいオブジェクトが作られ、遅いイメージがあります。実際には速度を計ってみないと何とも言えません。

Deque<Integer> temp = new ArrayDeque<>() を使う手もあります。
https://docs.oracle.com/javase/jp/8/docs/api/java/util/ArrayDeque.html

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.

コメントありがとうございます。

キューとして使われるときはLinkedListよりも高速です。

おっしゃる通りだと思いました。

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