Skip to content

108 convert sorted array to binary search tree#24

Open
tarinaihitori wants to merge 3 commits into
mainfrom
108-convert-sorted-array-to-binary-search-tree
Open

108 convert sorted array to binary search tree#24
tarinaihitori wants to merge 3 commits into
mainfrom
108-convert-sorted-array-to-binary-search-tree

Conversation

@tarinaihitori
Copy link
Copy Markdown
Owner

root_node.right = self.sortedArrayToBST(nums[mid+1:])

return root_node

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

良いと思いました。
再帰用の関数内関数を作って、
配列ではなく、BSTを構築する範囲を与えるやり方もあると思います。

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

確かにインナー関数を使うとスライスを使わずに済むので計算量も少なくて良さそうです。

node_ranges.append((node.right, mid + 1, right_mid, right))

return root
```
Copy link
Copy Markdown

@Fuminiton Fuminiton Apr 1, 2025

Choose a reason for hiding this comment

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

node_rangesは(node, left, right)として実現する方が再帰の書き換えにもなっていますし、処理もシンプルになるように思います。

mid = len(nums) // 2
root = TreeNode(nums[mid])

node_ranges = [(root, 0, mid, len(nums) - 1)]
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

node_rangesの情報量がやや多いように思います。midはいらないかもしれません。範囲のインデックスを格納する以外にはnumsをスライスして格納する方法もあります。

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

再帰呼び出すの関数の引数部分がスタックに積まれるので、mid は pop した後に作ればいいですね。

node_queue.append((node.left, left, left_mid, mid - 1))

if mid + 1 <= right:
right_mid = (mid + 1 + right) // 2
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

midを使うことで実装が複雑になっているように見えます

mid = len(nums) // 2

root_node = TreeNode(nums[mid])
root_node.left = self.sortedArrayToBST(nums[:mid])
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

スライスはコピーにO(k)かかります。
https://wiki.python.org/moin/TimeComplexity

@@ -0,0 +1,124 @@
1st
時間計算量:O(n)
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

これ、スライスがコピーしているので log n がつきそうです。(各階層ごとに全体がコピーされていてそれが log n の深さ行われます。)

mid = len(nums) // 2
root = TreeNode(nums[mid])

node_ranges = [(root, 0, mid, len(nums) - 1)]
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

再帰呼び出すの関数の引数部分がスタックに積まれるので、mid は pop した後に作ればいいですね。

Comment on lines +85 to +86
mid = len(nums) // 2
root = TreeNode(nums[mid])
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

mid を後で計算するように変更すると、この二行も不要になります。

```python
class Solution:
def sortedArrayToBST(self, nums: List[int]) -> Optional[TreeNode]:
def _sortedArrayToBST(left:int, right:int)-> Optional[TreeNode]:
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

inner function の関数名の先頭には _ は付けないほうが多いと思います。 _ を付けるのは、クラスメソッドやモジュールメソッドのうち、他から呼ばれたくないもに対してだと思います。

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.

7 participants