Skip to content
Open
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
12 changes: 12 additions & 0 deletions 0102-binary-tree-level-order-traversal/memo.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
### step1

dfsで各ノードを走査しながら、もっている各階層のlevelのvector[level]にnodeをpush_backしていく。

### step2

名前がてきとうだったのでましな名前に変更。

### step3

ChatGPTに聞いたところ、「👉 レベル順探索 = BFSが本質的に一致」ということがわかったのでBFSで実装し直し。
3回通すまで書き直し。
36 changes: 36 additions & 0 deletions 0102-binary-tree-level-order-traversal/step1.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
* };
*/
class Solution {
public:
void dfs(TreeNode* root, int level, vector<vector<int>>& vvi) {
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

vvi という変数名は vector vector int の略でしょうか。変数名から、中にどのような値が含まれているのか想像しにくく感じました。

こちらのコメントをご参照ください。
#10 (comment)

また、型名をそのまま変数名に含めても、読み手にとって得られる情報はあまり増えないように感じました。変数名には、どのような値が格納されているかが分かる名前を付けると、より読みやすくなると思います。

nodes_by_level はいかがでしょうか?

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_by_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.

関数名は、動詞の原形または命令形で始まることが多いように思います。 traverse() はいかがでしょうか?

if (!root) {
return;
}
if (vvi.size() < (level + 1)) {
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

(level + 1) の () は冗長に感じました。

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.

そうですね、括弧なしにします。

vvi.resize(level + 1);
}
vvi[level].push_back(root->val);
if (root->left) {
dfs(root->left, level + 1, vvi);
}
if (root->right) {
dfs(root->right, level + 1, vvi);
}
}

vector<vector<int>> levelOrder(TreeNode* root) {
int level = 0;
vector<vector<int>> vvi;
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

vviとはどういう意図の命名でしょうか??

Copy link
Copy Markdown
Owner Author

@hemispherium hemispherium Apr 20, 2026

Choose a reason for hiding this comment

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

とりあえず付けたてきとうな名前ですね。一応 vector<vector< int >> を短くしてvviみたいな感じです。step2でnodesという名前にrenameしています。

dfs(root, level, vvi);
return vvi;
}
};
36 changes: 36 additions & 0 deletions 0102-binary-tree-level-order-traversal/step2.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
* };
*/
class Solution {
public:
void scan(TreeNode* root, int level, vector<vector<int>>& nodes) {
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

scan という英単語は、一列に並んでいるものを走査するというニュアンスを感じます。グラフ構造を辿っていくのであれば traverse() あたりが良いと思います。

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.

そうなんですね。traverseの方を使うようにします。

if (!root) {
return;
}
if (nodes.size() < (level + 1)) {
nodes.resize(level + 1);
}
nodes[level].push_back(root->val);
if (root->left) {
scan(root->left, level + 1, nodes);
}
if (root->right) {
scan(root->right, level + 1, nodes);
}
}

vector<vector<int>> levelOrder(TreeNode* root) {
int level = 0;
vector<vector<int>> nodes;
scan(root, level, nodes);
return nodes;
}
};
40 changes: 40 additions & 0 deletions 0102-binary-tree-level-order-traversal/step3.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
* };
*/
class Solution {
public:
vector<vector<int>> levelOrder(TreeNode* root) {
vector<vector<int>> result;
if (!root) return result;

queue<TreeNode*> nodes;
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

kitano-kazuki/leetcode#21 (comment)

キューに異なる階層のノードが入っていて、かつそれらの深さはキュー外で管理されている、という構造がちょっと違和感がありました。私なら、いま見る階層と、次に見る階層を分けてスワップします。dequeが不要になるのもメリットです。

BFSでは, 階層ごとに配列を用意する方法があります。最近は読みやすいので自分もこっちを使っています

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

@hemispherium さん
参考までにお聞きしたいのですが、

キューに異なる階層のノードが入っていて、かつそれらの深さはキュー外で管理されている

という書き方を最初に見たのはどこか覚えていますか?

nodes.push(root);

while (!nodes.empty()) {
int size = nodes.size();
vector<int> level;

for (int i = 0; i < size; i++) {
TreeNode* node = nodes.front();
nodes.pop();

level.push_back(node->val);

if (node->left) nodes.push(node->left);
if (node->right) nodes.push(node->right);
}

result.push_back(level);
}

return result;
}
};