Conversation
MaximumDepthOfBinaryTree/step1.md
Outdated
| return maxDepthFindByBfs(root); | ||
| } | ||
|
|
||
| public int maxDepthFindByDfs(TreeNode root, int depth) { |
There was a problem hiding this comment.
maxDepthFindByDfs でも良いと思うのですが、やや冗長に思いました。 traverse はいかがでしょうか?
MaximumDepthOfBinaryTree/step1.md
Outdated
| return maxDepthFindByBfs(root); | ||
| } | ||
|
|
||
| public int maxDepthFindByDfs(TreeNode root, int depth) { |
There was a problem hiding this comment.
depth を持たせず、
if (root == null) {
return 0;
}
とし、呼び出し時に戻り値に + 1 して return したほうがシンプルだと思います。この場合、 maxDepth() 関数をそのまま再帰呼び出しするだけで実装でき、実装量も減ります。
|
|
||
| int maxDepth = 0; | ||
| while (!queue.isEmpty()) { | ||
| int queueSize = queue.size(); |
There was a problem hiding this comment.
queue のサイズをあらかじめ取得し、そのサイズ分だけ処理をするという処理は、普段見ないように思います。 queue の中に TreeNode と 深さの両方を入れるか、現在処理中のキューと、次に処理予定のキューに分けるほうが一般的だと思います。
There was a problem hiding this comment.
木構造の階層ごとに処理を挟みたいからこの処理にしています。
Pairで深さを入れる方が良さそうですね。
MaximumDepthOfBinaryTree/step1.md
Outdated
| maxDepth++; | ||
| } | ||
|
|
||
| return maxDepth++; |
There was a problem hiding this comment.
maxDepth++ はインクリメントする前の値を返します。よって ++ は不要だと思います。
MaximumDepthOfBinaryTree/step1.md
Outdated
| if (root == null) { | ||
| return depth; | ||
| } | ||
| return Math.max(maxDepthFindByDfs(root.left, depth + 1), maxDepthFindByDfs(root.right, depth + 1)); |
There was a problem hiding this comment.
1 行が長すぎるように感じます。チームのコーディング規約に合わせ、適宜改行したほうが良いと思います。 Google Java Style Guide では 1 行 100 文字までとされています。
https://google.github.io/styleguide/javaguide.html
MaximumDepthOfBinaryTree/step1.md
Outdated
| if (root == null) { | ||
| return 0; | ||
| } | ||
| return Math.max(maxDepth(root.left) + 1, maxDepth(root.right) + 1); |
There was a problem hiding this comment.
return Math.max(maxDepth(root.left), maxDepth(root.right)) + 1; のほうがシンプルだと思います。
| maxDepth = Math.max(current.getValue(), maxDepth); | ||
|
|
||
| if (current.getKey().left != null) { | ||
| queue.add(new Pair(current.getKey().left, current.getValue() + 1)); |
There was a problem hiding this comment.
Pair の使用は極力避けましょう。理由は、 key と value に何が含まれているかをソースコードから読み取るのにやや認知負荷がかかるためです。
class TreeNodeAndDepth {
public TreeNode treeNode;
public int depth;
}
等、ユーザー型を定義し、それに入れたほうが読みやすいです。
There was a problem hiding this comment.
プロダクトに使うことでしたらそうしますが、面接の時は定義の実装少し面倒なため、一言普段こうやって書きませんよってやんわり流しながらPair使うのは許されるものでしょうか?
MaximumDepthOfBinaryTree/step2.md
Outdated
|
|
||
| int nextDepth = currentDepth + 1; | ||
| return Math.max(maxDepthFindByDfs(root.left, nextDepth) | ||
| , maxDepthFindByDfs(root.right, nextDepth)); |
There was a problem hiding this comment.
改行をするにあたり、先頭に , を持ってくる書き方はあまり見ないように思います。
return Math.max(maxDepthFindByDfs(root.left, nextDepth),
maxDepthFindByDfs(root.right, nextDepth));
あたりが良いと思います。
MaximumDepthOfBinaryTree/step2.md
Outdated
| ```java | ||
| class Solution { | ||
| public int maxDepth(TreeNode root) { | ||
| return maxDepthFindByBfs(root, 1); |
There was a problem hiding this comment.
間違っていますね。多分LeetCodeからコピーしてから色々書き換えてた時の残骸です。修正しました。 🙇
https://leetcode.com/problems/maximum-depth-of-binary-tree/description/