Skip to content
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

104. Maximum Depth of Binary Tree #35

Merged
merged 1 commit into from
Mar 9, 2025
Merged

104. Maximum Depth of Binary Tree #35

merged 1 commit into from
Mar 9, 2025

Conversation

colorbox
Copy link
Owner

@colorbox colorbox commented Jan 13, 2025

https://leetcode.com/problems/maximum-depth-of-binary-tree/

leetcode_kuzuhara 再帰いいんじゃないですか。 BFS、数数えていくんだったら、 queue> にするか queue current_queue; queue next_queue; として、queue::swap していく方法などがありますかね。

変数名もクラス名もキャメルにしてますが、snake, camel にするなどのほうが一般的かもしれません。いずれにせよ、統一されていればいいです。
https://google.github.io/styleguide/cppguide.html#Variable_Names

https://discord.com/channels/1084280443945353267/1183683738635346001/1200833413540741192

https://github.com/hayashi-ay/leetcode/pull/22/files
shining-ai/leetcode#21

BFSなのでmax取らなくても良いですね。
shining-ai/leetcode#21 (comment)

rossy0213/leetcode#10
nittoco/leetcode#14
fhiyo/leetcode#23
SuperHotDogCat/coding-interview#34
Yoshiki-Iwasa/Arai60#23
kazukiii/leetcode#22
Ryotaro25/leetcode_first60#23
goto-untrapped/Arai60#45
seal-azarashi/leetcode#20
hroc135/leetcode#20
rihib/leetcode#41
haniwachann/leetcode#4
tarinaihitori/leetcode#21

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) {
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.

なるほど、ありがとうございます。

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を利用する判断をしました。

public:
int maxDepth(TreeNode* root) {
stack<pair<TreeNode*, int>> nodes_and_depths;
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.

なるほど、ありがとうございます、たしかにそちらのほうがシンプルになりますね

@@ -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.

あー、なるほど、確かに、ありがとうございます。

@colorbox colorbox merged commit 37a31dc into main Mar 9, 2025
@colorbox colorbox deleted the 104 branch March 9, 2025 14:34
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