-
Notifications
You must be signed in to change notification settings - Fork 0
104. Maximum Depth of Binary Tree #35
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| /* | ||
| Solve Time : 2:56 | ||
|
|
||
| Time : O(V + E) | ||
| Space : O(V) | ||
|
|
||
| 特に問題なく解いた、ひとまず一番シンプルにかける再帰を使用したが、queueやstackを使用したほうがよいはず。 | ||
| */ | ||
| class Solution { | ||
| public: | ||
| int maxDepth(TreeNode* root) { | ||
| if (root == nullptr) { | ||
| return 0; | ||
| } | ||
| const int left_depth = maxDepth(root->left); | ||
| const int right_depth = maxDepth(root->right); | ||
| return max(left_depth, right_depth) + 1; | ||
| } | ||
| }; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| /* | ||
| Time : O(V + E) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. グラフに対するDFSの一般的な時間計算量はO(V+E)ですが、二分木の場合、Eが高々2Vで抑えられる(平衡二分木の時にEが最大になる)ため、O(V)と書いても大丈夫だと思います
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. あー、なるほど、確かに、ありがとうございます。 |
||
| Space : O(V) | ||
| */ | ||
| class Solution { | ||
| public: | ||
| int maxDepth(TreeNode* root) { | ||
| stack<pair<TreeNode*, int>> nodes_and_depths; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 人によっては pair の使用を原則避ける場合があります。理由は、中にどのような値が含まれているかが分かりにくいためです。代わりに構造体を定義し、それを使うようにするようです。
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. コメントありがとうございます、今回は値を取り出すときに構造化束縛などで可読性を担保できるのでpairを利用する判断をしました。 |
||
| nodes_and_depths.emplace(root, 0); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 自分なら
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. なるほど、ありがとうございます、たしかにそちらのほうがシンプルになりますね |
||
| int max_depth = 0; | ||
| while (!nodes_and_depths.empty()) { | ||
| auto [node, depth] = nodes_and_depths.top(); | ||
| nodes_and_depths.pop(); | ||
| if (node == nullptr) { | ||
| continue; | ||
| } | ||
| max_depth = max(max_depth, depth + 1); | ||
| nodes_and_depths.emplace(node->left, depth + 1); | ||
| nodes_and_depths.emplace(node->right, depth + 1); | ||
| } | ||
| return max_depth; | ||
| } | ||
| }; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| class Solution { | ||
| public: | ||
| int maxDepth(TreeNode* root) { | ||
| queue<pair<TreeNode*, int>> nodes_and_depths; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. depthをqueueに入れない方法もありますね。 class Solution {
public:
int maxDepth(TreeNode* root) {
if (!root) {
return 0;
}
queue<TreeNode*> que({root});
int depth = 0;
for (; !que.empty(); ++depth) {
int level_size = que.size();
for (int i = 0; i < level_size; ++i) {
TreeNode* node = que.front();
que.pop();
if (node->left) {
que.push(node->left);
}
if (node->right) {
que.push(node->right);
}
}
}
return depth;
}
};class Solution {
public:
int maxDepth(TreeNode* root) {
if (!root) {
return 0;
}
vector<TreeNode*> level({root});
vector<TreeNode*> next_level;
int depth = 0;
for (; !level.empty(); ++depth) {
while (!level.empty()) {
auto node = level.back();
level.pop_back();
if (node->left) {
next_level.push_back(node->left);
}
if (node->right) {
next_level.push_back(node->right);
}
}
level.swap(next_level);
}
return depth;
}
};
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. なるほど、ありがとうございます。 |
||
| nodes_and_depths.emplace(root, 0); | ||
| int max_depth = 0; | ||
| while (!nodes_and_depths.empty()) { | ||
| auto [node, depth] = nodes_and_depths.front(); | ||
| nodes_and_depths.pop(); | ||
| max_depth = depth; | ||
| if (!node) { | ||
| continue; | ||
| } | ||
| nodes_and_depths.emplace(node->left, depth + 1); | ||
| nodes_and_depths.emplace(node->right, depth + 1); | ||
| } | ||
| return max_depth; | ||
| } | ||
| }; | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
これでもいいでしょう。上から行くのと下から行くのが一応あって、それぞれ再帰とループがあります。
ただ、そこまでこの問題は考えなくてもいいかもしれません。
https://docs.google.com/document/d/11HV35ADPo9QxJOpJQ24FcZvtvioli770WWdZZDaLOfg/edit?tab=t.0#heading=h.27jfjzhov3la