Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 additions & 0 deletions arai60/convert-sorted-array-to-binary-search-tree/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
## 考察
- 初見の問題
- 方針
- 真ん中がroot、その左側がleft subtree、右側がright subtreeとなるので十分
- あとは再帰的に構築できる
- height-balancedなので、木の高さはlogオーダー
- よって再帰処理で書いてOK
- time: O(n), space: O(n)
- dummyの値を持ったcomplete binary treeを構築して、inorder traversalしながら値をセットしても良さそう

Choose a reason for hiding this comment

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

あ、なるほど〜
inorder traversalの性質をうまく使ってますね 👀

- BSTをinorder traversalすると、昇順ソートになるため
- 1ステップ挟むため少しコードが長くなりそう
- time: O(n), space: O(n)
- まずは最初の方針を実装して、Step2でもう一方のアイデアをやってみる

## Step1
- 上で考えたアルゴリズムを実装
- time: O(n), space: O(n)

## Step2
- 上で考えたもう一方のアルゴリズムも実装
- 他の人のPRを検索
- https://github.com/fhiyo/leetcode/pull/26
- 方針はだいたい良さそう
- 半開区間でやっている
- これは二分探索というのかな?
- https://github.com/fhiyo/leetcode/pull/26#discussion_r1651955390
- たしかに見る区間を半々にはしているが
- midという形容詞について
- https://github.com/fhiyo/leetcode/pull/26#discussion_r1654034973
- Boxについて
- https://github.com/fhiyo/leetcode/pull/26#discussion_r1652886883
- 初めて見た
- 参照をラップしている?一般的な概念なのかな?後ほど詳しく調べる
Comment on lines +30 to +33
Copy link

Choose a reason for hiding this comment

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

一般的な概念なのかはよく分からないです...オブジェクトを持てればListでも何でも構わないですが、何となくこのときは独自クラスを作りました
ちなみにBoxの元ネタはRustのBox で、ヒープ上に値を確保するときに使うやつです

Copy link
Owner Author

Choose a reason for hiding this comment

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

元ネタはRustなんですね。ありがとうございます!

Copy link

Choose a reason for hiding this comment

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

Box はもうちょっと広い古い概念かとおもいます。Java でもよく使ってましたね。
https://en.wikipedia.org/wiki/Boxing_(computer_science)

Copy link

Choose a reason for hiding this comment

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

ありがとうございます。Boxing, ふわっとしたイメージしか無かったのでありがたいです
(Java触ったことあるなら知っていて良い概念ですね...)

- https://github.com/Mike0121/LeetCode/pull/13
- 方針はだいたい良さそう
- 閉区間、半開区間ともに書いている

## Step3
- 1回目: 2m30s
- 2回目: 1m40s
- 3回目: 1m37s
17 changes: 17 additions & 0 deletions arai60/convert-sorted-array-to-binary-search-tree/step1.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
class Solution:
def sortedArrayToBST(self, nums: List[int]) -> Optional[TreeNode]:
def build_bst(left: int, right: int) -> Optional[TreeNode]:
if right - left < 0: return None
mid = (left + right) // 2
root = TreeNode(nums[mid])
root.left = build_bst(left, mid - 1)
root.right = build_bst(mid + 1, right)
return root

return build_bst(0, len(nums) - 1)
27 changes: 27 additions & 0 deletions arai60/convert-sorted-array-to-binary-search-tree/step2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
class Solution:
Copy link

Choose a reason for hiding this comment

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

こちらの解法は思いつきませんでした。おそらく step1・3 が想定解な気がします。

def sortedArrayToBST(self, nums: List[int]) -> Optional[TreeNode]:
def build_complete_binary_tree(index: int) -> Optional[TreeNode]:
if index >= len(nums): return None
node = TreeNode()
node.left = build_complete_binary_tree(2 * index + 1)
node.right = build_complete_binary_tree(2 * index + 2)
return node

binary_tree = build_complete_binary_tree(0)
index = 0
def set_value(root: Optional[TreeNode]) -> None:
nonlocal index
if not root: return
set_value(root.left)
root.val = nums[index]
index += 1
set_value(root.right)

set_value(binary_tree)
return binary_tree
17 changes: 17 additions & 0 deletions arai60/convert-sorted-array-to-binary-search-tree/step3.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
class Solution:
def sortedArrayToBST(self, nums: List[int]) -> Optional[TreeNode]:
def build_bst(left: int, right: int) -> Optional[TreeNode]:
if right - left == 0: return None

Choose a reason for hiding this comment

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

if left >= right: return None 好みかもしれないですが。自分はこちらのほうが好きです。

mid = (left + right) // 2
root = TreeNode(nums[mid])
root.left = build_bst(left, mid)
root.right = build_bst(mid + 1, right)
return root

return build_bst(0, len(nums))