-
Notifications
You must be signed in to change notification settings - Fork 0
solved construct_binary_tree_from_preorder_and_inorder_traversal #43
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: arai60
Are you sure you want to change the base?
solved construct_binary_tree_from_preorder_and_inorder_traversal #43
Conversation
|
|
||
| class Solution: | ||
| def buildTree(self, preorder: List[int], inorder: List[int]) -> Optional[TreeNode]: | ||
| preorder_index = -1 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
preorder_index が副作用なのは追いにくく、確かにこう書くこともありますが、個人的にはどれくらい消費したかを返り値で返して欲しいですね。
あとはコメント集にいくつか他の解法もあった気がしますが、おまけですね。
https://docs.google.com/document/d/11HV35ADPo9QxJOpJQ24FcZvtvioli770WWdZZDaLOfg/edit?tab=t.0#heading=h.1rv0z8fm6lc3
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
副作用を直したバージョンを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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ここの4行の変数名と中身の整合性が取れていない気がしました。自分なら以下のようにします
root_value = preorder[0]
root = TreeNode(root_value)
root_inorder_index = inorder.index(root_value)There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
気になった点は、
root_idはidではなくvalueを取得しているはずindexだけだと読み進めて何のindexだったのか忘れてしまったroot_idという変数を元にnodeを生成しているが、rootで良いのではnodeを宣言してから使うまでに距離がある
辺りです
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ありがとうございます。直してみました
| 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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
好みの問題だと思いますが、このように対照関係がある場合には自分はcontinueでインデントを下げる方法よりもif,elseを使いたくなります
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
同じ感覚です。continueは早期returnに近いイメージがあります。
自然言語で表現されているのがこちらですかね。機械の使用中止のところです。
https://discord.com/channels/1084280443945353267/1192728121644945439/1194203372115464272
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
私は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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
関数名が 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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
preorderをスライスするのは分かりにくい気もしますが、発想は面白く感じました。
問題: https://leetcode.com/problems/construct-binary-tree-from-preorder-and-inorder-traversal/?envType=problem-list-v2&envId=xo2bgr0r