Skip to content

543. Diameter of Binary Tree#21

Open
ryosuketc wants to merge 1 commit intomainfrom
543_diameter_of_binary_tree
Open

543. Diameter of Binary Tree#21
ryosuketc wants to merge 1 commit intomainfrom
543_diameter_of_binary_tree

Conversation

@ryosuketc
Copy link
Owner

def traverse(node):
if node is None:
return 0
nonlocal diameter
Copy link

Choose a reason for hiding this comment

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

inner function の内部で使用する外側の変数を、 inner function よりあとに書くと、変数の定義を確認するために目線を上下に移動しなければならなくならず、読み手にとって煩わしく感じられる場合があります。 inner function の手前で定義することをおすすめします。

private:
int diameter;

int LongestPath(TreeNode* node) {
Copy link

Choose a reason for hiding this comment

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

関数名は動詞の命令形 (原形) から始めることが多いと思います。

* };
*/
class Solution {
private:
Copy link

Choose a reason for hiding this comment

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

読み手は、外部からアクセス可能な public: が指定されている関数・変数の定義に興味があると思います。クラスの定義を読んで初めに目に入る上のほうに、 public: な関数・変数を置くことをおすすめします。

参考までにスタイルガイドへのリンクを貼ります。

https://google.github.io/styleguide/cppguide.html#Declaration_Order

Group similar declarations together, placing public parts earlier.

上記のスタイルガイドは唯一絶対のルールではなく、複数あるスタイルガイドの一つに過ぎないということを念頭に置くことをお勧めします。また、所属するチームにより何が良いとされているかは変わります。自分の中で良い書き方の基準を持ちつつ、チームの平均的な書き方で書くことをお勧めいたします。

int right_path = LongestPath(node->right);
diameter = std::max(diameter, left_path + right_path + 2);
return std::max(left_path, right_path) + 1;

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants