Skip to content

Create 111. Minimum Depth of Binary Tree#30

Open
Apo-Matchbox wants to merge 1 commit intomainfrom
Create-111.-Minimum-Depth-of-Binary-Tree
Open

Create 111. Minimum Depth of Binary Tree#30
Apo-Matchbox wants to merge 1 commit intomainfrom
Create-111.-Minimum-Depth-of-Binary-Tree

Conversation

@Apo-Matchbox
Copy link
Copy Markdown
Owner

- ドキュメントを確認したが、よく理解できなかった。
- q.front()自体は参照だけど、一度[e]というローカル変数へコピーされる
- nodeとdepthはそのコピーeのメンバへの参照
- queueの中身には結びついていない。こんな理解でいいのかな
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

それで問題ないかと思います。
auto& で受けると中への参照です。

return 0;
}

std::queue<std::pair<TreeNode*, int>> q;
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

変数名qについて、自分はfrontiersにするかなと思いました。これから見る予定のものを詰め込んでいくという感覚で使っています。
LeetCodeの回答のような短い処理では変数名qでも問題ないと思いますが、コーディング練習の観点から語彙の提案という目的でコメントしました。

Copy link
Copy Markdown

@naoto-iwase naoto-iwase left a comment

Choose a reason for hiding this comment

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

どちらのコードも読みやすいと感じました。


### 解法を考える。
- maximumと同じようにDFS実行すると、勿体無いのでBFSで最小深さがいいのか
- DFSでもmin(minDepth(root->left), minDepth(root->right))で求められるのか
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

DFSでも、根からの深さを持ちながら全探索を行い、葉に到達するたびにmin_depth = min(min_depth, depth)と更新することで最小深さを求められますね。

return 0;
}

std::vector<TreeNode*> current_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.

current_nodesだと個人的に情報量が少ないと感じるので、自分ならnodes_in_depthやfrontiersなどと名付けます。

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.

@docto-rin
frontiersの命名をする人が多いですね。参考にさせて頂きます。

@5103246
Copy link
Copy Markdown

5103246 commented Dec 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.

5 participants