Conversation
| } | ||
| int depth = MeasureDepth(root); | ||
| return depth; | ||
| } |
There was a problem hiding this comment.
この関数、単に return MeasureDepth(root); でよくないでしょうか。としたら、二つを一つにしてもいいのではないでしょうか。
| */ | ||
| class Solution { | ||
| public: | ||
| int MeasureDepth(TreeNode* node) { |
There was a problem hiding this comment.
この関数を公開する必要がないように見えました。privateでいいのかなと思います。
There was a problem hiding this comment.
メンバー変数使わないのと、引数のノードも変更しないので、以下のように書けます。
static int MeasureDepth(TreeNode* node)
また、maxDepthとMeasureDepthは、呼び出し側からすると完全に同じ振る舞いをします。一つにまとめて良さそうです。
There was a problem hiding this comment.
@fhiyo @liquo-rice
レビューありがとうございます。staticを使うことも覚えておきます。
指摘の通り分ける必要ないので今回は一つに纏めました。なのでMeasureDepth関数自体削除しました。
| if (root == nullptr) { | ||
| return 0; | ||
| } |
There was a problem hiding this comment.
@fhiyo
関数側でチェックしているので必要ないですね。今回は他の指摘であったことから関数自体削除しましたのでメイン処理側では残しました。
| */ | ||
| class Solution { | ||
| public: | ||
| int MeasureDepth(TreeNode* node) { |
There was a problem hiding this comment.
メンバー変数使わないのと、引数のノードも変更しないので、以下のように書けます。
static int MeasureDepth(TreeNode* node)
また、maxDepthとMeasureDepthは、呼び出し側からすると完全に同じ振る舞いをします。一つにまとめて良さそうです。
| if (node == nullptr) { | ||
| continue; | ||
| } | ||
| max_depth = max(depth, max_depth); |
There was a problem hiding this comment.
私もここは常にdepthのほうが大きいと思います.
max_depth = depthでも同じことになると思います.
There was a problem hiding this comment.
@liquo-rice @fhiyo
常に更新されているので意味無かったですね。修正しました。
|
指摘あるところ以外良さそうです |
| */ | ||
| class Solution { | ||
| public: | ||
| int maxDepth(TreeNode* root) { |
There was a problem hiding this comment.
Leetcode の都合上変えられないので仕方ないですが、引数に渡される TreeNode がルートノードなのは最初だけなので、これを root とするのは適当でないように思いました。
There was a problem hiding this comment.
各関数呼び出しを考えると、そのnodeを根とするツリーのmaxDepthを求めているという意味ではありなのかなと思います
その場合、left_tree_max_depth, right_tree_max_depthと変数名をつけることが適当でしょうか(長いか)
There was a problem hiding this comment.
各関数呼び出しを考えると、そのnodeを根とするツリーのmaxDepthを求めているという意味ではありなのかなと思います
あーなるほどその発想はありませんでした。そう考えると確かに不自然ではないかもしれないです。
There was a problem hiding this comment.
その場合、left_tree_max_depth, right_tree_max_depthと変数名をつけることが適当でしょうか(長いか)
Java ならまあありそうだなって感じの長さに思いました。 C++ だとどうなんでしょうね
問題へのリンク
https://leetcode.com/problems/maximum-depth-of-binary-tree/description/
問題文(プレミアムの場合)
備考
次に解く問題の予告
Minimum Depth of Binary Tree
フォルダ構成
LeetCodeの問題ごとにフォルダを作成します。
フォルダ内は、step1.cpp、step2.cpp、step3.cpp、bfs.cppとmemo.mdとなります。
memo.md内に各ステップで感じたことを追記します。