Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions 104/step1.cpp
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を使用したほうがよいはず。
Copy link

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

*/
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;
}
};
23 changes: 23 additions & 0 deletions 104/step2_1.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/*
Time : O(V + E)
Copy link

Choose a reason for hiding this comment

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

グラフに対するDFSの一般的な時間計算量はO(V+E)ですが、二分木の場合、Eが高々2Vで抑えられる(平衡二分木の時にEが最大になる)ため、O(V)と書いても大丈夫だと思います

Copy link
Owner Author

Choose a reason for hiding this comment

The 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;
Copy link

Choose a reason for hiding this comment

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

人によっては pair の使用を原則避ける場合があります。理由は、中にどのような値が含まれているかが分かりにくいためです。代わりに構造体を定義し、それを使うようにするようです。
十分に読みやすいのであれば、 pair を使ってもまったく問題ないと思います。今回は、変数名に pair の中に何が含まれているかが分かるような名前が付けられており、かつ auto [node, depth] = nodes_and_depths.top(); と、中に何が入っているかが想像しやすいコードがあるため、まったく問題ないと思います。

Copy link
Owner Author

Choose a reason for hiding this comment

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

コメントありがとうございます、今回は値を取り出すときに構造化束縛などで可読性を担保できるのでpairを利用する判断をしました。

nodes_and_depths.emplace(root, 0);
Copy link

Choose a reason for hiding this comment

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

自分なら.emplace(root, 1)にすると思いました

Copy link
Owner Author

Choose a reason for hiding this comment

The 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;
}
};
19 changes: 19 additions & 0 deletions 104/step3.cpp
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;

Choose a reason for hiding this comment

The 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;
    }
};

Copy link
Owner Author

Choose a reason for hiding this comment

The 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;
}
};