Open
Conversation
arahi10
reviewed
Mar 11, 2026
|
|
||
| 再帰的にサブツリーを構成していけば良いと考えた。サブツリーの具体的な構成方法について | ||
| 配列の中央値を根とすれば良い。 | ||
| 時間計算量: O(N) => 各要素が1回ずつ処理されるので -> あってる? |
There was a problem hiding this comment.
リストのスライスを作る計算手間もありますね。https://wiki.python.org/moin/TimeComplexity#:~:text=O(n)-,Get%20Slice,-O(k
There was a problem hiding this comment.
計算量は
( $ 2T(N/2) $ … 半分にした配列を引数にした再帰呼び出し2回分
$ O(N) $ … スライスの分)
を満たすので
arahi10
reviewed
Mar 11, 2026
| ## Step2 | ||
|
|
||
| - https://github.com/irohafternoon/LeetCode/pull/28/changes#r2061089474 | ||
| >この問題だとHelperを使わない再帰の書き方もありますね。そのほうがコンパクトになり、またtotalに関する議論もなくなるので個人的には好きですが、他の問題ではHelperを使ったほうが見通し良いものもあるので、単なる好みの範疇かもしれません。 |
There was a problem hiding this comment.
そもそもヘルパー関数要らないと思うのですが、ヘルパー関数にした意図って何でしょうか?
class Solution:
def sortedArrayToBST(self, nums: List[int]) -> Optional[TreeNode]:
if len(nums) == 1:
return TreeNode(nums[0])
if len(nums) == 0:
return None
mid = len(nums) // 2
root = TreeNode(nums[mid])
left = nums[:mid]
right = nums[mid+1:]
root.left = self.sortedArrayToBST(left)
root.right = self.sortedArrayToBST(right)
return root
arahi10
reviewed
Mar 11, 2026
|
|
||
| 再帰的にサブツリーを構成していけば良いと考えた。サブツリーの具体的な構成方法について | ||
| 配列の中央値を根とすれば良い。 | ||
| 時間計算量: O(N) => 各要素が1回ずつ処理されるので -> あってる? |
There was a problem hiding this comment.
計算量は
( $ 2T(N/2) $ … 半分にした配列を引数にした再帰呼び出し2回分
$ O(N) $ … スライスの分)
を満たすので
| if not nums: | ||
| return None | ||
|
|
||
| dummy = TreeNode() |
There was a problem hiding this comment.
この番兵置くなら上の if not num はなくても問題なく動きそうですね。
nodchip
reviewed
Mar 14, 2026
| ```py | ||
| class Solution: | ||
| def sortedArrayToBST(self, nums: List[int]) -> Optional[TreeNode]: | ||
| def construct_bintree(left_idx, right_idx): |
There was a problem hiding this comment.
こちらのコメントをご参照ください。
hemispherium/LeetCode_Arai60#10 (comment)
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.
解く問題
Convert Sorted Array To Binary Search Tree
次に解く問題
Path Sum