Skip to content

Conversation

@SuperHotDogCat
Copy link
Owner


class Solution:
def buildTree(self, preorder: List[int], inorder: List[int]) -> Optional[TreeNode]:
preorder_index = -1
Copy link

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

Copy link
Owner Author

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
Copy link

@olsen-blue olsen-blue Feb 17, 2025

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」の更新のタイミングを後ろにするだけでこの点解消できるので、ご検討ください。

hayashi-ay/leetcode#43

node = TreeNode()
root_id = preorder[0]
index = inorder.index(root_id)
node.val = root_id
Copy link

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)

Copy link

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を宣言してから使うまでに距離がある
    辺りです

Copy link
Owner Author

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))
Copy link

Choose a reason for hiding this comment

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

好みの問題だと思いますが、このように対照関係がある場合には自分はcontinueでインデントを下げる方法よりもif,elseを使いたくなります

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

Copy link

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:
Copy link

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

Copy link

Choose a reason for hiding this comment

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

preorderをスライスするのは分かりにくい気もしますが、発想は面白く感じました。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants