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;

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