Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions 102/solution1_1.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/**
* 自力解法。10分ほどでAC 時間計算量: O(n), 空間計算量: O(n) n:全要素数 思考プロセスとしては、各階層のものを順にみていくのでBFSが一番直感的だと思ったのでそれで実装。
* 階層の区切りにQueueを使った。
*/
public class solution1_1 {
public List<List<Integer>> levelOrder(TreeNode root) {
List<List<Integer>> values = new ArrayList<>();
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.

ありがとうございます!

nodes.offer(root);
while (!nodes.isEmpty()) {
Queue<TreeNode> nodesInNextLevel = new ArrayDeque<>();
List<Integer> valuesInNextLevel = new ArrayList<>();
while (!nodes.isEmpty()) {
Comment on lines +13 to +16
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でやる感じですかね

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かなと思っていたのですが認識相違ありますでしょうか?

if (current.left != null) {
nodesInNextLevel.offer(current.left);
}
if (current.right != null) {
nodesInNextLevel.offer(current.right);
}
}
values.add(valuesInNextLevel);
nodes = nodesInNextLevel;
}
return values;
}
}
28 changes: 28 additions & 0 deletions 102/solution2_1.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/**
* LeetCodeの解法を参考にしたもの。 DFSでの解法。 BFSでなくともlevelで引数として渡せばOKという解法. stackつかってもできる。
* 今回は、再帰の深さは最大で要素数nとなり、問題の制約は2000なのでまあ多分大丈夫かくらい
*
* <p>https://github.com/t0hsumi/leetcode/pull/26/files
* https://github.com/seal-azarashi/leetcode/pull/25/files でも大体同じ解法
*/
public class solution2_1 {
public List<List<Integer>> levelOrder(TreeNode root) {
List<List<Integer>> values =
new ArrayList<>(); // https://github.com/t0hsumi/leetcode/pull/26/filesとかだとlevel_ordered_valuesにしてもう少し具体的にしてる。
levelOrderHelper(root, 0, values);
return values;
}

private void levelOrderHelper(TreeNode node, int level, List<List<Integer>> values) {
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だとより明示的ですね

values.add(new ArrayList<>());
}
List<Integer> list = values.get(level);
list.add(node.val);
Comment on lines +23 to +24
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.

ありがとうございます!

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行でしか使われていないのであえて宣言する必要もそんなにないとも思いました。

levelOrderHelper(node.left, level + 1, values);
levelOrderHelper(node.right, level + 1, values);
}
}
20 changes: 20 additions & 0 deletions 102/solution3_1.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/** いただいたコメントを元にした解法 再帰部分での条件式をより読み手に伝わり例外なくListがある状態になることが一目でわかるようにwhileを使用。(読み手への配慮) */
public class solution3_1 {
public List<List<Integer>> levelOrder(TreeNode root) {
List<List<Integer>> values = new ArrayList<>();
levelOrderHelper(root, 0, values);
return values;
}

private void levelOrderHelper(TreeNode node, int level, List<List<Integer>> values) {
if (node == null) {
return;
}
while (values.size() <= level) {
values.add(new ArrayList<>());
}
values.get(level).add(node.val);
levelOrderHelper(node.left, level + 1, values);
levelOrderHelper(node.right, level + 1, values);
}
}