-
Notifications
You must be signed in to change notification settings - Fork 0
108. Convert Sorted Array to Binary Search Tree #13
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
return sortedArrayToBST(nums, 0, nums.length - 1); | ||
} | ||
|
||
public TreeNode sortedArrayToBST(int[] nums, int start, int end) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
開閉区間でも書いてみていただけますか?
There was a problem hiding this comment.
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]
There was a problem hiding this comment.
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() を実装したバージョンを書いてみていただけますでしょうか?
There was a problem hiding this comment.
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) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
23 行目以降でカバーできているため、この if 文はいらないと思います。
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
子が存在しないケースで early return
のつもりで入れてました。
There was a problem hiding this comment.
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]); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
これ、なくても動きますか? (ちゃんと考えていない。)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
動きます。こちらの指摘と同じ疑問になったっていうところですか?
#13 (comment)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
あ、そうですね。単に長くなっているので特別扱いする理由がない感覚です。
https://leetcode.com/problems/convert-sorted-array-to-binary-search-tree/description/