-
Notifications
You must be signed in to change notification settings - Fork 0
543. Diameter of Binary Tree #5
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
base: main
Are you sure you want to change the base?
Conversation
|
|
||
| int maxDepth(struct TreeNode *root, int *diameter) | ||
| { | ||
| int l, r, d; |
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.
変数の宣言をスコープの先頭にまとめて書く書き方は、古い C 言語みが感じられて違和感があります。
C99 以降では戦闘以外でも宣言できます。変数のスコープを短くし、読みやすくするため、初めて使用した個所で宣言することをお勧めします。
if (root == NULL)
return 0;
int l = maxDepth(root->left, diameter);
int r = maxDepth(root->right, diameter);
int d = l + r;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.
なるほど!取り入れていきます!🫡
|
|
||
| int maxDepth(struct TreeNode *root, int *diameter) | ||
| { | ||
| int l, r, d; |
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.
変数名には原則、変数の値を表す端的な英単語・英語句をフルスペルで付けることをお勧めします。ただし、スコープが十分に短い場合は、 1 文字変数でも十分だと思います。
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.
なるほど!
フルスペルなんですね。
例えば、strcpyなどで仮引数名をdst srcにするのですがそれよりもdestination sourceのほうが良いんですかね(?_?)
これは私の書いたものではなくて引用です🙇♂
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.
チームの平均的なメンバーが最短で正確に認知負荷少なく読める書き方であれば、どちらでも大丈夫だと思います。チーム内で dst・src といった略語が多用されているのであれば、 dst・src で良いと思います。
|
|
||
|  | ||
|
|
||
| 0ms!? |
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.
LeetCode の出力する処理時間は、計測誤差がかなり大きいようです。あまり当てにしないほうがよいと思います。
もしより正確に処理時間を計測したい場合は、手元で十分な回数処理を実行して処理時間を計測し、中央値を取ることをお勧めします。
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.
Time.h のtime()を使用していこうと思います。
|  | ||
|
|
||
| 0ms!? | ||
| こんな計算量ちゃうの!? |
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.
計算量は、与えられたデータサイズに対して、おおよそどれくらいの計算ステップで処理ができるかを表す概念です。ここで計算量という単語を使うのはやや違和感があります。
計算時間がより適切でしょうか。
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.
確かに、言葉の誤用ですね。ご指摘ありがとうございます!
言葉の使い方などもぜひプロの感覚と合わせていきたいですね。
| * struct TreeNode *right; | ||
| * }; | ||
| */ | ||
| int diameterOfBinaryTree(struct TreeNode *root) |
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.
自分はあまり C 言語は書かないのですが、戻り値の型のあとの空白をタブで入れるのはあまり見ないように思います。これは有名なスタイルなのでしょうか?
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.
42で使用されているNorminetteというFormat基準です。
私は今年の3月に42Tokyoというところで初めてプログラミングを始めたので、今でもその書き方を使用しています。
| ``` | ||
|
|
||
| なんかすげぇ。 | ||
| インライン展開?初めて見た。 |
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://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 | |||
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.
変数の横方向の位置を揃える目的以外で、タブを連続しておくことはあまりないように思います。また、変数の横方向の位置を揃えること自体も、 diff が見づらくなるため、最近ではあまりやらないようです。
| int rightDepth; | ||
|
|
||
| if (!root) | ||
| return (0); |
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.
return する値を () で囲むのは、あまり見ないように思います。 1975年 の C 言語の構文の仕様からくるスタイルのようです。書かないスタイルのほうをよく見かけます。
https://qiita.com/yohhoy/items/3f8fb2da1d96a7b5ba42?utm_source=chatgpt.com
| right = right->right; | ||
| rightCount++; | ||
| } | ||
| } No newline at end of file |
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.
ファイルの末尾は改行で終えることをお勧めいたします。ファイルの末尾に表示されるマークを見逃さないようにすることをお勧めいたします。
| ```c | ||
| #define MAX(x, y) ((x) > (y) ? (x) : (y)) | ||
|
|
||
| int maxDepth(struct TreeNode *root, int *diameter) |
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.
変数名を lower_snake で、関数名を UpperCamel で各スタイルもあります。所属するチームの平均的な書き方で書くことをお勧めいたします。
| こんな計算量ちゃうの!? | ||
| 全然わかんねぇ。 | ||
|
|
||
| あーけど、1回目の提出のやつは毎回再帰の中で再帰してるから、O(n^2)になるんか。 |
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.r1pr4pvhjsjx
| return (0); | ||
| diameter = 0; | ||
| getDepth(root, &diameter); | ||
| return (maxDiameter); |
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.
これ、打ち間違いですか?
This problem
https://leetcode.com/problems/diameter-of-binary-tree/description/
next problem
https://leetcode.com/problems/maximum-candies-you-can-get-from-boxes/description/?envType=daily-question&envId=2025-06-03