Conversation
nodchip
reviewed
Apr 14, 2024
| return sortedArrayToBST(nums, 0, nums.length - 1); | ||
| } | ||
|
|
||
| public TreeNode sortedArrayToBST(int[] nums, int start, int end) { |
Owner
Author
There was a problem hiding this comment.
すみません、質問が理解できてなくて、こいうものを求めてるんでしょうか?
親のノードがnums[mid]だとしたときに、
左に入るものは [min_value, nums[mid]]
右に入るものは [nums[mid], max_value]
There was a problem hiding this comment.
最初の解法は [start, end] の閉区間で解いています。これとは別に、 [start, end) の半開区間で解きなおしていただけますでしょうか?
別の言い方をすると、
return sortedArrayToBST(nums, 0, nums.length);
と呼び出すよう、 sortedArrayToBST() を実装したバージョンを書いてみていただけますでしょうか?
| return null; | ||
| } | ||
|
|
||
| if (start == end) { |
Owner
Author
There was a problem hiding this comment.
子が存在しないケースで early return のつもりで入れてました。
There was a problem hiding this comment.
今回のケースでは、 early return をしても、コードが読みやすくなっていないように感じられます。 if 文を入れないほうがシンプルに感じました。
oda
reviewed
Apr 14, 2024
| } | ||
|
|
||
| if (l == r) { | ||
| return new TreeNode(nums[l]); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
https://leetcode.com/problems/convert-sorted-array-to-binary-search-tree/description/