Conversation
| return 1 + std::max(MeasureTreeDepth(node->right), | ||
| MeasureTreeDepth(node->left)); | ||
| } | ||
| }; |
There was a problem hiding this comment.
こちらの方が自分も好みです。
step1は、ベースケースでdepth - 1が変える部分が煩雑な感じがしますよね。
| next_nodes.push_back(node->left); | ||
| } | ||
| } | ||
| std::swap(current_nodes, next_nodes); |
There was a problem hiding this comment.
depthは1からスタートさせて、更新をswapの前後にするのもいいと思います。
「深さXのループが回って、最後にX+1の探索リストを受け渡してもらう」という流れが自然な気がするので。
| next_nodes.push_back(node->right); | ||
| } | ||
| } | ||
| std::swap(current_nodes, next_nodes); |
There was a problem hiding this comment.
current_nodes.swap(next_nodes); というのもあります。どちらでもいいでしょう。
There was a problem hiding this comment.
ありがとうございます。
std::swap と std::vector::swapは別(std::vector::swapはstd::vectorのメンバ関数)
ただ、std::swapをvectorに対して使うと実質std::vector::swapを使ったことになる、と理解しました。
https://cplusplus.com/reference/vector/vector/swap/
std::swapは実質3回moveをして付け替える操作と理解しましたが、vectorのswapが何かはあまり分かりませんでした。
https://cplusplus.com/reference/algorithm/swap/
|
|
||
| #### 2週目の宿題 | ||
| - 配列(vectorを自分で作ってみる)今回とは関係ないが、やっていないなと思ったので | ||
| - そもそも二分探索木の実装をしてみる(BITなど) |
There was a problem hiding this comment.
There was a problem hiding this comment.
ありがとうございます。赤黒木はセジウィックCアルゴリズムにも載っていたと記憶しているので、参照しながら、余裕のある時にじっくり取り組めれば良いと思っています
| while (!current_nodes.empty()) { | ||
| depth++; | ||
| std::vector<TreeNode*> next_nodes; | ||
| for (auto node : current_nodes) { |
There was a problem hiding this comment.
ありがとうございます。
STEP4書き直しの際に修正いたします。
This Problem
Maximum Depth of Binary Tree
Next Problem
Minimum Depth of Binary Tree