solved construct_binary_tree_from_preorder_and_inorder_traversal#43
solved construct_binary_tree_from_preorder_and_inorder_traversal#43SuperHotDogCat wants to merge 3 commits intoarai60from
Conversation
|
|
||
| class Solution: | ||
| def buildTree(self, preorder: List[int], inorder: List[int]) -> Optional[TreeNode]: | ||
| preorder_index = -1 |
There was a problem hiding this comment.
preorder_index が副作用なのは追いにくく、確かにこう書くこともありますが、個人的にはどれくらい消費したかを返り値で返して欲しいですね。
あとはコメント集にいくつか他の解法もあった気がしますが、おまけですね。
https://docs.google.com/document/d/11HV35ADPo9QxJOpJQ24FcZvtvioli770WWdZZDaLOfg/edit?tab=t.0#heading=h.1rv0z8fm6lc3
There was a problem hiding this comment.
副作用を直したバージョンをphase4.pyに追加しました
| # 副作用のあるhelper関数を許す | ||
| class Solution: | ||
| def buildTree(self, preorder: List[int], inorder: List[int]) -> Optional[TreeNode]: | ||
| preorder_index = -1 |
There was a problem hiding this comment.
preorder_indexの初期値ですが、私もこの問題にStep1で取り組んだ時には -1 にしていました。
しかし、インデックスが -1 というのは、パッと見で、末尾を指定しているようにも見えなくないなということが、その後気になり修正しました。「+= 1」の更新のタイミングを後ろにするだけでこの点解消できるので、ご検討ください。
| node = TreeNode() | ||
| root_id = preorder[0] | ||
| index = inorder.index(root_id) | ||
| node.val = root_id |
There was a problem hiding this comment.
ここの4行の変数名と中身の整合性が取れていない気がしました。自分なら以下のようにします
root_value = preorder[0]
root = TreeNode(root_value)
root_inorder_index = inorder.index(root_value)There was a problem hiding this comment.
気になった点は、
root_idはidではなくvalueを取得しているはずindexだけだと読み進めて何のindexだったのか忘れてしまったroot_idという変数を元にnodeを生成しているが、rootで良いのではnodeを宣言してから使うまでに距離がある
辺りです
There was a problem hiding this comment.
ありがとうございます。直してみました
| continue | ||
| back = self.search_parent_of_right_child(inorder_split_index, stack) | ||
| back.node.right = node | ||
| stack.append(InorderPosition(node, inorder_split_index, back.right_limit)) |
There was a problem hiding this comment.
好みの問題だと思いますが、このように対照関係がある場合には自分はcontinueでインデントを下げる方法よりもif,elseを使いたくなります
There was a problem hiding this comment.
同じ感覚です。continueは早期returnに近いイメージがあります。
自然言語で表現されているのがこちらですかね。機械の使用中止のところです。
https://discord.com/channels/1084280443945353267/1192728121644945439/1194203372115464272
There was a problem hiding this comment.
私はcontinueのほうが好みです。
if-elseだと後で使う可能性があるので脳のメモリを解放できないのが理由です。
| stack.append(InorderPosition(node, inorder_split_index, back.right_limit)) | ||
| return dummy.left | ||
|
|
||
| def search_parent_of_right_child(self, inorder_split_index: int, stack: list[InorderPosition]) -> InorderPosition: |
There was a problem hiding this comment.
関数名が search で始まっていると、引数に対して変更を加えない印象があります。引数に対して変更を加えていることを明示的に表す名前のほうがよいと思います。
| root.left = self.buildTree(preorder[1:1 + root_inorder_index], inorder[:root_inorder_index]) | ||
| root.right = self.buildTree(preorder[1 + root_inorder_index:], inorder[root_inorder_index + 1:]) | ||
| return root | ||
|
|
There was a problem hiding this comment.
preorderをスライスするのは分かりにくい気もしますが、発想は面白く感じました。
問題: https://leetcode.com/problems/construct-binary-tree-from-preorder-and-inorder-traversal/?envType=problem-list-v2&envId=xo2bgr0r