Skip to content

104. Maximum Depth of Binary Tree#10

Open
rossy0213 wants to merge 1 commit intomainfrom
arai60-21
Open

104. Maximum Depth of Binary Tree#10
rossy0213 wants to merge 1 commit intomainfrom
arai60-21

Conversation

@rossy0213
Copy link
Copy Markdown
Owner

return maxDepthFindByBfs(root);
}

public int maxDepthFindByDfs(TreeNode root, int depth) {
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

maxDepthFindByDfs でも良いと思うのですが、やや冗長に思いました。 traverse はいかがでしょうか?

return maxDepthFindByBfs(root);
}

public int maxDepthFindByDfs(TreeNode root, int depth) {
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

depth を持たせず、

if (root == null) {
    return 0;
}

とし、呼び出し時に戻り値に + 1 して return したほうがシンプルだと思います。この場合、 maxDepth() 関数をそのまま再帰呼び出しするだけで実装でき、実装量も減ります。

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

確かに、修正しました。


int maxDepth = 0;
while (!queue.isEmpty()) {
int queueSize = queue.size();
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

queue のサイズをあらかじめ取得し、そのサイズ分だけ処理をするという処理は、普段見ないように思います。 queue の中に TreeNode と 深さの両方を入れるか、現在処理中のキューと、次に処理予定のキューに分けるほうが一般的だと思います。

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

木構造の階層ごとに処理を挟みたいからこの処理にしています。
Pairで深さを入れる方が良さそうですね。

maxDepth++;
}

return maxDepth++;
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

maxDepth++ はインクリメントする前の値を返します。よって ++ は不要だと思います。

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

タイプでした。修正しました。

if (root == null) {
return depth;
}
return Math.max(maxDepthFindByDfs(root.left, depth + 1), maxDepthFindByDfs(root.right, depth + 1));
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

1 行が長すぎるように感じます。チームのコーディング規約に合わせ、適宜改行したほうが良いと思います。 Google Java Style Guide では 1 行 100 文字までとされています。
https://google.github.io/styleguide/javaguide.html

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

修正しました。

if (root == null) {
return 0;
}
return Math.max(maxDepth(root.left) + 1, maxDepth(root.right) + 1);
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

return Math.max(maxDepth(root.left), maxDepth(root.right)) + 1; のほうがシンプルだと思います。

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

あ、確かにそうですね。

maxDepth = Math.max(current.getValue(), maxDepth);

if (current.getKey().left != null) {
queue.add(new Pair(current.getKey().left, current.getValue() + 1));
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Pair の使用は極力避けましょう。理由は、 key と value に何が含まれているかをソースコードから読み取るのにやや認知負荷がかかるためです。

class TreeNodeAndDepth {
    public TreeNode treeNode;
    public int depth;
}

等、ユーザー型を定義し、それに入れたほうが読みやすいです。

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

プロダクトに使うことでしたらそうしますが、面接の時は定義の実装少し面倒なため、一言普段こうやって書きませんよってやんわり流しながらPair使うのは許されるものでしょうか?

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

はい、大丈夫だと思います。


int nextDepth = currentDepth + 1;
return Math.max(maxDepthFindByDfs(root.left, nextDepth)
, maxDepthFindByDfs(root.right, nextDepth));
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

改行をするにあたり、先頭に , を持ってくる書き方はあまり見ないように思います。

return Math.max(maxDepthFindByDfs(root.left, nextDepth),
        maxDepthFindByDfs(root.right, nextDepth));

あたりが良いと思います。

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

確認漏れです。修正しました。

```java
class Solution {
public int maxDepth(TreeNode root) {
return maxDepthFindByBfs(root, 1);
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

これ、引数あってます?

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

間違っていますね。多分LeetCodeからコピーしてから色々書き換えてた時の残骸です。修正しました。 🙇

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants