From 0f5349d5c358c4e31df7c0d10fd03a926d03d729 Mon Sep 17 00:00:00 2001 From: kazukiii Date: Wed, 10 Jul 2024 01:39:24 -0700 Subject: [PATCH] =?UTF-8?q?step1,=20step2,=20step3=E3=82=92=E8=BF=BD?= =?UTF-8?q?=E5=8A=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../README.md | 41 +++++++++++++++++++ .../step1.py | 17 ++++++++ .../step2.py | 27 ++++++++++++ .../step3.py | 17 ++++++++ 4 files changed, 102 insertions(+) create mode 100644 arai60/convert-sorted-array-to-binary-search-tree/README.md create mode 100644 arai60/convert-sorted-array-to-binary-search-tree/step1.py create mode 100644 arai60/convert-sorted-array-to-binary-search-tree/step2.py create mode 100644 arai60/convert-sorted-array-to-binary-search-tree/step3.py diff --git a/arai60/convert-sorted-array-to-binary-search-tree/README.md b/arai60/convert-sorted-array-to-binary-search-tree/README.md new file mode 100644 index 0000000..609e89c --- /dev/null +++ b/arai60/convert-sorted-array-to-binary-search-tree/README.md @@ -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しながら値をセットしても良さそう + - 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 + - 初めて見た + - 参照をラップしている?一般的な概念なのかな?後ほど詳しく調べる + - https://github.com/Mike0121/LeetCode/pull/13 + - 方針はだいたい良さそう + - 閉区間、半開区間ともに書いている + +## Step3 +- 1回目: 2m30s +- 2回目: 1m40s +- 3回目: 1m37s diff --git a/arai60/convert-sorted-array-to-binary-search-tree/step1.py b/arai60/convert-sorted-array-to-binary-search-tree/step1.py new file mode 100644 index 0000000..4233317 --- /dev/null +++ b/arai60/convert-sorted-array-to-binary-search-tree/step1.py @@ -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) diff --git a/arai60/convert-sorted-array-to-binary-search-tree/step2.py b/arai60/convert-sorted-array-to-binary-search-tree/step2.py new file mode 100644 index 0000000..b6789ab --- /dev/null +++ b/arai60/convert-sorted-array-to-binary-search-tree/step2.py @@ -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: + 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 diff --git a/arai60/convert-sorted-array-to-binary-search-tree/step3.py b/arai60/convert-sorted-array-to-binary-search-tree/step3.py new file mode 100644 index 0000000..dc2371a --- /dev/null +++ b/arai60/convert-sorted-array-to-binary-search-tree/step3.py @@ -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) + root.right = build_bst(mid + 1, right) + return root + + return build_bst(0, len(nums))