Open
Conversation
nodchip
reviewed
Nov 15, 2025
| def traverse(node): | ||
| if node is None: | ||
| return 0 | ||
| nonlocal diameter |
There was a problem hiding this comment.
inner function の内部で使用する外側の変数を、 inner function よりあとに書くと、変数の定義を確認するために目線を上下に移動しなければならなくならず、読み手にとって煩わしく感じられる場合があります。 inner function の手前で定義することをおすすめします。
| private: | ||
| int diameter; | ||
|
|
||
| int LongestPath(TreeNode* node) { |
| * }; | ||
| */ | ||
| class Solution { | ||
| private: |
There was a problem hiding this comment.
読み手は、外部からアクセス可能な 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; | ||
|
|
There was a problem hiding this comment.
この空行は処理の区切りを視覚的に分かりやすくしたもの等ではく、読み手にとって有益な情報を与えないため、無いほうが良いと思います。
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
543. Diameter of Binary Tree
https://leetcode.com/problems/diameter-of-binary-tree/