Skip to content
Open
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
118 changes: 118 additions & 0 deletions tree-bst/102.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
- [102. Binary Tree Level Order Traversal](https://leetcode.com/problems/binary-tree-level-order-traversal/description/)
- 方針
- Queue
- 階層ごとにリストに node.val を詰めていけば良いと考えた
- 時間計算量: O(N), 空間計算量: O(N)
- 実装時間: 1st 20分、2nd 20分
- 再帰関数
- 2000程度なのでコールスタックも耐えられそうだけど、解法が思いつかない
- 左右に分離していく時に同じ階層の要素を揃えて取得するコードを書くのは難しそう。。
- Stack
- Queue と同じような実装になりそう。Queue 実装で今日は時間切れ。

## BFS
```java
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode() {}
* TreeNode(int val) { this.val = val; }
* TreeNode(int val, TreeNode left, TreeNode right) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
class Solution {
public List<List<Integer>> levelOrder(TreeNode root) {
List<List<Integer>> result = new ArrayList<>();

if (root == null) {
return result;
}

Deque<TreeNodeAndDepth> nodes = new ArrayDeque<>();
int depth = 1;
nodes.addLast(new TreeNodeAndDepth(root, 1));

while (!nodes.isEmpty()) {
List<Integer> temp = new ArrayList<>();
while (nodes.peekFirst() != null
&& nodes.peekFirst().depth == depth) {
TreeNodeAndDepth nodeAndDepth = nodes.pollFirst();

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

インデントが深すぎるかと思います。

@hiroki-horiguchi-dev hiroki-horiguchi-dev Jun 7, 2026

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.

ありがとうございます、そうですね。
2ndでだいぶ削れたので、修正できている認識です。

上記↑じゃなくて、シンプルにインデントが深いって話ですかね。。
であれば、leetcode のサイト上で解いたので条件箇所と分けるために一段インデント深くしてました。

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 (nodes.peekFirst() != null
                    && nodes.peekFirst().depth == depth) {
                TreeNodeAndDepth nodeAndDepth = nodes.pollFirst();

TreeNode node = nodeAndDepth.treeNode;
temp.add(node.val);

if (node.left != null) {
nodes.add(new TreeNodeAndDepth(
node.left, nodeAndDepth.depth + 1)
);
}

if (node.right != null) {
nodes.add(new TreeNodeAndDepth(node.right,
nodeAndDepth.depth + 1));
}
}
depth++;
result.add(temp);
}

return result;
}

private record TreeNodeAndDepth(TreeNode treeNode, int depth){}
}
```
- もう少し綺麗にしたい、特に depth を外に持つ必要はない
- 取り出す時、queue に存在するサイズ分二重ループを回せば良いだけだった
- record も不要
```java
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode() {}
* TreeNode(int val) { this.val = val; }
* TreeNode(int val, TreeNode left, TreeNode right) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
class Solution {
public List<List<Integer>> levelOrder(TreeNode root) {
List<List<Integer>> result = new ArrayList<>();
if (root == null) {
return result;
}
Deque<TreeNode> nodes = new ArrayDeque<>();
nodes.addLast(root);
while (!nodes.isEmpty()) {
int size = nodes.size();
List<Integer> tempList = new ArrayList<>();

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

自分なら valsInLevel と名付けると思います。 vals でも通じると思います。

for (int i = 0; i < size; i++) {
TreeNode node = nodes.pollFirst();
tempList.add(node.val);
if (node.left != null) {
nodes.add(node.left);
}

if (node.right != null) {
nodes.add(node.right);
}
}
result.add(tempList);
}

return result;
}
}
```