Skip to content

111. Minimum Depth of Binary Tree - #22

Open
hiroki-horiguchi-dev wants to merge 1 commit into
mainfrom
tree-bst-111
Open

111. Minimum Depth of Binary Tree#22
hiroki-horiguchi-dev wants to merge 1 commit into
mainfrom
tree-bst-111

Conversation

@hiroki-horiguchi-dev

Copy link
Copy Markdown
Owner

Comment thread tree-bst/111.md
Comment on lines +36 to +45
int left = minDepth(root.left);
int right = minDepth(root.right);

if (root.left != null && root.right == null) {
return left + 1;
}

if (root.left == null && root.right != null) {
return 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.

条件分岐は以下の方がわかりやすいと思いました。

if (root.left == null) {
    return minDepth(root.right) + 1;
}
if (root.right == null) {
    return minDepth(root.left) + 1;
}

return Math.min(minDepth(root.left), minDepth(root.right)) + 1;

@hiroki-horiguchi-dev hiroki-horiguchi-dev Jun 7, 2026

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.

コメントの返信が遅くなりすみません。
ありがとうございます、おっしゃる通りですね。
方針部分で書いたように、leaf Node と brach node を分けて書くことを意識した結果、コメントいただいたコードになりました。

が、コメントいただいたコードの方が冗長な分岐と変数がなく、スッキリしていてわかりやすいですね。
「自然言語で方針を説明できる --> 手作業をそのまま起こしたようなコードを書く --> 冗長な箇所を削る」を含めて短時間でできるように意識していきたいと思います。

@hiroki-horiguchi-dev hiroki-horiguchi-dev self-assigned this Jun 7, 2026
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.

2 participants