Skip to content

Create Maximum Depth of Binary Tree.md#23

Open
irohafternoon wants to merge 1 commit intomainfrom
104.-Maximum-Depth-of-Binary-Tree
Open

Create Maximum Depth of Binary Tree.md#23
irohafternoon wants to merge 1 commit intomainfrom
104.-Maximum-Depth-of-Binary-Tree

Conversation

@irohafternoon
Copy link
Copy Markdown
Owner

return 1 + std::max(MeasureTreeDepth(node->right),
MeasureTreeDepth(node->left));
}
};
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

こちらの方が自分も好みです。
step1は、ベースケースでdepth - 1が変える部分が煩雑な感じがしますよね。

next_nodes.push_back(node->left);
}
}
std::swap(current_nodes, next_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.

depthは1からスタートさせて、更新をswapの前後にするのもいいと思います。
「深さXのループが回って、最後にX+1の探索リストを受け渡してもらう」という流れが自然な気がするので。

next_nodes.push_back(node->right);
}
}
std::swap(current_nodes, next_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.swap(next_nodes); というのもあります。どちらでもいいでしょう。

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.

ありがとうございます。
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など)
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

授業でやるのは平衡二分探索木かもしれませんが書くのがとにかく大変なんですよね。
https://discord.com/channels/1084280443945353267/1183683738635346001/1185264362508795984
https://discord.com/channels/1084280443945353267/1237649827240742942/1253382452802490431

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.

ありがとうございます。赤黒木はセジウィックCアルゴリズムにも載っていたと記憶しているので、参照しながら、余裕のある時にじっくり取り組めれば良いと思っています

while (!current_nodes.empty()) {
depth++;
std::vector<TreeNode*> next_nodes;
for (auto node : 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.

インデントがずれているようです。

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.

ありがとうございます。
STEP4書き直しの際に修正いたします。

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.

4 participants