diff --git a/problems/108.convert-sorted-array-to-binary-search-tree/memo.md b/problems/108.convert-sorted-array-to-binary-search-tree/memo.md new file mode 100644 index 0000000..274e034 --- /dev/null +++ b/problems/108.convert-sorted-array-to-binary-search-tree/memo.md @@ -0,0 +1,30 @@ +## step1 +- 難しく感じるがEasyなのか +- 配列を前から見て適切な位置に1つ1つ入れていくのがいいのかなと思うが、Example1の状態で-11,-12,-13と入れていく場合はどうすればいいかわからない + - 平衡である必要がない場合は書けた + - わからないのでChatGPTに聞く + - 常に中央の値を親にして小さい値を左、大きい値を右に再帰していく +- 常に平衡二分木を保ちつつデータを追加していく(DBのIndexで使われているような話を聞いたことがある)ようにできた方が良いと勝手に思い込んで先頭から足す方法にこだわってしまった + + +## step2 +- 他の人のコードを読む +- https://github.com/05ryt31/leetcode/pull/15 + - コードの出力は自分と似ていたので感覚は近そう +- https://github.com/mamo3gr/arai60/pull/23/changes + - 配列の範囲の終点を扱うのに、開区間と閉区間の方がある(始点は閉区間でいいだろう)。Pythonのインデックスに従うなら開区間だが、閉区間の方が(個人的には)扱いやすくバグのリスクが減らせそう。「リーダブルコードで」、開区間なら `begin, end`, 閉区間なら `start, last` を使う、という命名を思い出す。→書いてみたら「中央の要素を抜いた、配列の左右がそれぞれ存在するかどうか」の分岐条件が違うだけみたいだった。 + - 命名の部分勉強になった +- https://github.com/Shoichifunyu/shofun/pull/18 + - 外部要因とかやむを得ない部分以外でtry-catchを使うことが自分的には違和感があった + - 自分が制御できる範囲や正常系の範囲の中でErrorを使用するのに自分は抵抗がある感じ + - `build_tree関数の返り値の型がTreeNode(非Optional)になるように書くこともでき、再帰呼び出しの回数がおよそ半減します。趣味の範囲です。` + - buildを使う再帰の場合Noneを省けるので自分で書く際には条件が絞られ思考がシンプルになりそうだと思った + - 理解しながら書いてみた + - 配列ごと分けるのかndexで分けるのかでも違いがある +- 取り組む際に早めにあきらめてChatGPTに答えを聞いてしまったことで受け身で学ぶ体験になってしまったので、無理矢理でも途中まででも自分でコードを書けばよかったと思った + +## step3 +- 一度慣れると自然な感じがしてきた +- sliceの範囲が含むかどうかを毎回Interfaceモードを起動して具体例から調べてしまうので早く慣れたい + - a[0,1,2,3,4,5]としたときに a[:2]が[0,1]なのか[0,1,2]なのか毎回わからなくなる。。。 + diff --git a/problems/108.convert-sorted-array-to-binary-search-tree/step1.py b/problems/108.convert-sorted-array-to-binary-search-tree/step1.py new file mode 100644 index 0000000..dd2842b --- /dev/null +++ b/problems/108.convert-sorted-array-to-binary-search-tree/step1.py @@ -0,0 +1,28 @@ +# +# @lc app=leetcode id=108 lang=python3 +# +# [108] Convert Sorted Array to Binary Search Tree +# + +# @lc code=start +# 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]: + if not nums: + return None + + mid = len(nums) // 2 + root = TreeNode(nums[mid]) + + root.left = self.sortedArrayToBST(nums[:mid]) + root.right = self.sortedArrayToBST(nums[mid + 1 :]) + + return root + + +# @lc code=end diff --git a/problems/108.convert-sorted-array-to-binary-search-tree/step1_wrong_answer.py b/problems/108.convert-sorted-array-to-binary-search-tree/step1_wrong_answer.py new file mode 100644 index 0000000..ff8e184 --- /dev/null +++ b/problems/108.convert-sorted-array-to-binary-search-tree/step1_wrong_answer.py @@ -0,0 +1,39 @@ +# +# @lc app=leetcode id=108 lang=python3 +# +# [108] Convert Sorted Array to Binary Search Tree +# + +# @lc code=start +# 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]: + binary_root = None + + def add_binary_node(binary_node, num): + if num < binary_node.val: + if binary_node.left is None: + binary_node.left = TreeNode(num) + else: + return add_binary_node(binary_node.left, num) + else: + if binary_node.right is None: + binary_node.right = TreeNode(num) + else: + return add_binary_node(binary_node.right, num) + + for num in nums: + if binary_root is None: + binary_root = TreeNode(num) + continue + add_binary_node(binary_root, num) + + return binary_root + + +# @lc code=end diff --git a/problems/108.convert-sorted-array-to-binary-search-tree/step2.py b/problems/108.convert-sorted-array-to-binary-search-tree/step2.py new file mode 100644 index 0000000..5a7bace --- /dev/null +++ b/problems/108.convert-sorted-array-to-binary-search-tree/step2.py @@ -0,0 +1,33 @@ +# +# @lc app=leetcode id=108 lang=python3 +# +# [108] Convert Sorted Array to Binary Search Tree +# + +# @lc code=start +# 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]: + if not nums: + return None + + def build_tree(left, right): + mid = (left + right) // 2 + + root = TreeNode(nums[mid]) + + if left < mid: + root.left = build_tree(left, mid - 1) + if mid < right: + root.right = build_tree(mid + 1, right) + return root + + return build_tree(0, len(nums) - 1) + + +# @lc code=end diff --git a/problems/108.convert-sorted-array-to-binary-search-tree/step3.py b/problems/108.convert-sorted-array-to-binary-search-tree/step3.py new file mode 100644 index 0000000..c5f6afd --- /dev/null +++ b/problems/108.convert-sorted-array-to-binary-search-tree/step3.py @@ -0,0 +1,28 @@ +# +# @lc app=leetcode id=108 lang=python3 +# +# [108] Convert Sorted Array to Binary Search Tree +# + +# @lc code=start +# 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]: + if not nums: + return None + + mid = len(nums) // 2 + + root = TreeNode(nums[mid]) + root.left = self.sortedArrayToBST(nums[:mid]) + root.right = self.sortedArrayToBST(nums[mid + 1 :]) + + return root + + +# @lc code=end