Skip to content

102. Binary Tree Level Order Traversal#29

Merged
ryoooooory merged 2 commits intomainfrom
task/102
Mar 19, 2025
Merged

102. Binary Tree Level Order Traversal#29
ryoooooory merged 2 commits intomainfrom
task/102

Conversation

@ryoooooory
Copy link
Copy Markdown
Owner

Comment thread 102/solution1_1.java
if (root == null) {
return values;
}
Queue<TreeNode> nodes = new ArrayDeque<>();
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

頭から尻尾まで舐めることしかしていないので、ArrayList でもいいかなと思いました。

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.

ありがとうございます!

Comment thread 102/solution2_1.java
if (node == null) {
return;
}
if (values.size() == 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.

上からしか呼ばないので、大きさが飛ばされることはないので、これでもいいですが、
while にするとアクセスする前にかならず、values.get(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.

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.

ありがとうございます!たしかにwhileだとより明示的ですね

Comment thread 102/solution1_1.java
Comment on lines +13 to +16
while (!nodes.isEmpty()) {
Queue<TreeNode> nodesInNextLevel = new ArrayDeque<>();
List<Integer> valuesInNextLevel = new ArrayList<>();
while (!nodes.isEmpty()) {
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

初見で同じwhile()が2個あって、意図を区別するまでに戸惑いがありました。
2個目の方はforで取り出すと、いいかもです。

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.

ありがとうございます!そのじてんでのQueueのsizeでやる感じですかね

Comment thread 102/solution2_1.java
Comment on lines +23 to +24
List<Integer> list = values.get(level);
list.add(node.val);
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

深さごとの部屋に追加していく操作は、こちらの解法でもありました。参考までに共有です。
t0hsumi/leetcode#26

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.

ありがとうございます!

Comment thread 102/solution1_1.java
List<Integer> valuesInNextLevel = new ArrayList<>();
while (!nodes.isEmpty()) {
TreeNode current = nodes.poll();
valuesInNextLevel.add(current.val);
Copy link
Copy Markdown

@goto-untrapped goto-untrapped Mar 12, 2025

Choose a reason for hiding this comment

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

valuesInCurrentLevel の気がしました。

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.

ありがとうございます!つぎの階層での探索になるのでNextかなと思っていたのですが認識相違ありますでしょうか?

Comment thread 102/solution2_1.java
values.add(new ArrayList<>());
}
List<Integer> list = values.get(level);
list.add(node.val);
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

javaのListが参照渡しなのでこのような書き方をしても動くと思うのですが、ぱっと見valuesの中身に変更が生じていることがわかりにくいので、values[level].add(node.val);と書いてもいいと思いました。listはこの2行でしか使われていないのであえて宣言する必要もそんなにないとも思いました。

@ryoooooory ryoooooory merged commit 7c86477 into main Mar 19, 2025
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