Skip to content

102. Binary Tree Level Order Traversal#25

Open
hemispherium wants to merge 2 commits into
mainfrom
0102-binary-tree-level-order-traversal
Open

102. Binary Tree Level Order Traversal#25
hemispherium wants to merge 2 commits into
mainfrom
0102-binary-tree-level-order-traversal

Conversation

@hemispherium
Copy link
Copy Markdown
Owner

@hemispherium hemispherium self-assigned this Apr 17, 2026

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しています。

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 さん
参考までにお聞きしたいのですが、

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

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

*/
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のような名前を付けるようにします。

*/
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.

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

*/
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 (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.

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

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.

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

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

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

@hemispherium
Copy link
Copy Markdown
Owner Author

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

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

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

これに関してはたぶんChatGPTに聞いたらそういう書き方をしてきたと思います。

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.

3 participants