Skip to content

Conversation

rossy0213
Copy link
Owner

return sortedArrayToBST(nums, 0, nums.length - 1);
}

public TreeNode sortedArrayToBST(int[] nums, int start, int end) {
Copy link

Choose a reason for hiding this comment

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

開閉区間でも書いてみていただけますか?

Copy link
Owner Author

Choose a reason for hiding this comment

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

すみません、質問が理解できてなくて、こいうものを求めてるんでしょうか?
親のノードがnums[mid]だとしたときに、
左に入るものは [min_value, nums[mid]]
右に入るものは [nums[mid], max_value]

Copy link

Choose a reason for hiding this comment

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

最初の解法は [start, end] の閉区間で解いています。これとは別に、 [start, end) の半開区間で解きなおしていただけますでしょうか?

別の言い方をすると、
return sortedArrayToBST(nums, 0, nums.length);
と呼び出すよう、 sortedArrayToBST() を実装したバージョンを書いてみていただけますでしょうか?

Copy link
Owner Author

Choose a reason for hiding this comment

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

この認識でいいんでしょうか?
77c7696

return null;
}

if (start == end) {
Copy link

Choose a reason for hiding this comment

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

23 行目以降でカバーできているため、この if 文はいらないと思います。

Copy link
Owner Author

Choose a reason for hiding this comment

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

子が存在しないケースで early return のつもりで入れてました。

Copy link

Choose a reason for hiding this comment

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

今回のケースでは、 early return をしても、コードが読みやすくなっていないように感じられます。 if 文を入れないほうがシンプルに感じました。

}

if (l == r) {
return new TreeNode(nums[l]);
Copy link

Choose a reason for hiding this comment

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

これ、なくても動きますか? (ちゃんと考えていない。)

Copy link
Owner Author

Choose a reason for hiding this comment

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

動きます。こちらの指摘と同じ疑問になったっていうところですか?
#13 (comment)

Copy link

Choose a reason for hiding this comment

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

あ、そうですね。単に長くなっているので特別扱いする理由がない感覚です。

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