Skip to content

Conversation


int maxDepth(struct TreeNode *root, int *diameter)
{
int l, r, d;
Copy link

Choose a reason for hiding this comment

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

変数の宣言をスコープの先頭にまとめて書く書き方は、古い C 言語みが感じられて違和感があります。
C99 以降では戦闘以外でも宣言できます。変数のスコープを短くし、読みやすくするため、初めて使用した個所で宣言することをお勧めします。

if (root == NULL)
    return 0;
int l = maxDepth(root->left, diameter);
int r = maxDepth(root->right, diameter);
int d = l + r;

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 maxDepth(struct TreeNode *root, int *diameter)
{
int l, r, d;
Copy link

Choose a reason for hiding this comment

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

変数名には原則、変数の値を表す端的な英単語・英語句をフルスペルで付けることをお勧めします。ただし、スコープが十分に短い場合は、 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.

なるほど!
フルスペルなんですね。
例えば、strcpyなどで仮引数名をdst srcにするのですがそれよりもdestination sourceのほうが良いんですかね(?_?)

これは私の書いたものではなくて引用です🙇‍♂

Copy link

Choose a reason for hiding this comment

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

チームの平均的なメンバーが最短で正確に認知負荷少なく読める書き方であれば、どちらでも大丈夫だと思います。チーム内で dst・src といった略語が多用されているのであれば、 dst・src で良いと思います。


![2回目の提出結果。]({C9285F2F-236B-4413-8BDE-89D96F81D01A}.png)

0ms!?
Copy link

Choose a reason for hiding this comment

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

LeetCode の出力する処理時間は、計測誤差がかなり大きいようです。あまり当てにしないほうがよいと思います。

もしより正確に処理時間を計測したい場合は、手元で十分な回数処理を実行して処理時間を計測し、中央値を取ることをお勧めします。

Copy link
Owner Author

Choose a reason for hiding this comment

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

Time.h のtime()を使用していこうと思います。

![2回目の提出結果。]({C9285F2F-236B-4413-8BDE-89D96F81D01A}.png)

0ms!?
こんな計算量ちゃうの!?
Copy link

Choose a reason for hiding this comment

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

計算量は、与えられたデータサイズに対して、おおよそどれくらいの計算ステップで処理ができるかを表す概念です。ここで計算量という単語を使うのはやや違和感があります。
計算時間がより適切でしょうか。

Copy link
Owner Author

Choose a reason for hiding this comment

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

確かに、言葉の誤用ですね。ご指摘ありがとうございます!
言葉の使い方などもぜひプロの感覚と合わせていきたいですね。

* struct TreeNode *right;
* };
*/
int diameterOfBinaryTree(struct TreeNode *root)
Copy link

Choose a reason for hiding this comment

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

自分はあまり C 言語は書かないのですが、戻り値の型のあとの空白をタブで入れるのはあまり見ないように思います。これは有名なスタイルなのでしょうか?

Copy link
Owner Author

Choose a reason for hiding this comment

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

42で使用されているNorminetteというFormat基準です。
私は今年の3月に42Tokyoというところで初めてプログラミングを始めたので、今でもその書き方を使用しています。

```

なんかすげぇ。
インライン展開?初めて見た。
Copy link

Choose a reason for hiding this comment

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

インライン展開は、関数を呼び出す側に呼び出される関数のコードを展開し、関数の呼び出しに掛かるコストをなくす、コンパイラーの最適化手法の一つです。
https://ja.wikipedia.org/wiki/%E3%82%A4%E3%83%B3%E3%83%A9%E3%82%A4%E3%83%B3%E5%B1%95%E9%96%8B

関数の再帰呼び出しと表現したかったのでしょうか。

@@ -0,0 +1,43 @@
struct TreeNode
Copy link

Choose a reason for hiding this comment

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

変数の横方向の位置を揃える目的以外で、タブを連続しておくことはあまりないように思います。また、変数の横方向の位置を揃えること自体も、 diff が見づらくなるため、最近ではあまりやらないようです。

int rightDepth;

if (!root)
return (0);
Copy link

Choose a reason for hiding this comment

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

return する値を () で囲むのは、あまり見ないように思います。 1975年 の C 言語の構文の仕様からくるスタイルのようです。書かないスタイルのほうをよく見かけます。

https://qiita.com/yohhoy/items/3f8fb2da1d96a7b5ba42?utm_source=chatgpt.com

right = right->right;
rightCount++;
}
} No newline at end of file
Copy link

Choose a reason for hiding this comment

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

ファイルの末尾は改行で終えることをお勧めいたします。ファイルの末尾に表示されるマークを見逃さないようにすることをお勧めいたします。

```c
#define MAX(x, y) ((x) > (y) ? (x) : (y))

int maxDepth(struct TreeNode *root, int *diameter)
Copy link

Choose a reason for hiding this comment

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

変数名を lower_snake で、関数名を UpperCamel で各スタイルもあります。所属するチームの平均的な書き方で書くことをお勧めいたします。

こんな計算量ちゃうの!?
全然わかんねぇ。

あーけど、1回目の提出のやつは毎回再帰の中で再帰してるから、O(n^2)になるんか。
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.r1pr4pvhjsjx

return (0);
diameter = 0;
getDepth(root, &diameter);
return (maxDiameter);
Copy link

Choose a reason for hiding this comment

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

これ、打ち間違いですか?

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