Skip to content

108: Convert Sorted Array To Binary Search Tree#29

Open
Yuto729 wants to merge 1 commit intomainfrom
convert-sorted-array-to-binary-search-tree
Open

108: Convert Sorted Array To Binary Search Tree#29
Yuto729 wants to merge 1 commit intomainfrom
convert-sorted-array-to-binary-search-tree

Conversation

@Yuto729
Copy link
Owner

@Yuto729 Yuto729 commented Mar 11, 2026

解く問題

Convert Sorted Array To Binary Search Tree

次に解く問題

Path Sum

@Yuto729 Yuto729 changed the title Convert Sorted Array To Binary Search Tree 108: Convert Sorted Array To Binary Search Tree Mar 11, 2026
Repository owner deleted a comment from github-actions bot Mar 11, 2026

再帰的にサブツリーを構成していけば良いと考えた。サブツリーの具体的な構成方法について
配列の中央値を根とすれば良い。
時間計算量: O(N) => 各要素が1回ずつ処理されるので -> あってる?
Copy link

@arahi10 arahi10 Mar 11, 2026

Choose a reason for hiding this comment

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

リストのスライスを作る計算手間もありますね。https://wiki.python.org/moin/TimeComplexity#:~:text=O(n)-,Get%20Slice,-O(k

Copy link

Choose a reason for hiding this comment

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

計算量は $N$ をリストのサイズとして
$T(N) = 2T(N/2) + O(N) $
( $ 2T(N/2) $ … 半分にした配列を引数にした再帰呼び出し2回分
$ O(N) $ … スライスの分)
を満たすので $O(N\log N)$ ですね

## Step2

- https://github.com/irohafternoon/LeetCode/pull/28/changes#r2061089474
>この問題だとHelperを使わない再帰の書き方もありますね。そのほうがコンパクトになり、またtotalに関する議論もなくなるので個人的には好きですが、他の問題ではHelperを使ったほうが見通し良いものもあるので、単なる好みの範疇かもしれません。
Copy link

Choose a reason for hiding this comment

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

そもそもヘルパー関数要らないと思うのですが、ヘルパー関数にした意図って何でしょうか?

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


再帰的にサブツリーを構成していけば良いと考えた。サブツリーの具体的な構成方法について
配列の中央値を根とすれば良い。
時間計算量: O(N) => 各要素が1回ずつ処理されるので -> あってる?
Copy link

Choose a reason for hiding this comment

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

計算量は $N$ をリストのサイズとして
$T(N) = 2T(N/2) + O(N) $
( $ 2T(N/2) $ … 半分にした配列を引数にした再帰呼び出し2回分
$ O(N) $ … スライスの分)
を満たすので $O(N\log N)$ ですね

if not nums:
return None

dummy = TreeNode()
Copy link

Choose a reason for hiding this comment

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

この番兵置くなら上の if not num はなくても問題なく動きそうですね。

```py
class Solution:
def sortedArrayToBST(self, nums: List[int]) -> Optional[TreeNode]:
def construct_bintree(left_idx, right_idx):
Copy link

Choose a reason for hiding this comment

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

こちらのコメントをご参照ください。
hemispherium/LeetCode_Arai60#10 (comment)

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