Skip to content

Conversation

@docto-rin
Copy link
Owner

@docto-rin docto-rin commented Oct 23, 2025

def buildTree(self, preorder: List[int], inorder: List[int]) -> Optional[TreeNode]:
inorder_value_to_index = {}
for i, value in enumerate(inorder):
inorder_value_to_index[value] = i
Copy link

Choose a reason for hiding this comment

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

{value: index for index, value in enumerate(inorder)} みたいな書き方ができたかもしれません。

Copy link
Owner Author

Choose a reason for hiding this comment

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

ありがとうございます。

内包表記ですね。

prosはコンパクトなこと、consは若干目が左右に揺れること、でしょうか。自分は使わないほうが好みでした。

preorder_index += 1

inorder_index = inorder_value_to_index[node.val]
subtrees.append((inorder_index + 1, inorder_last, node, False))
Copy link

@potrue potrue Oct 23, 2025

Choose a reason for hiding this comment

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

inorderのindexで入れるかどうかの判断はこの段階でしたいかもしれません。subtreesに入る順番がpreorderの順番と一致するというのはロジックの重要な部分だと思うので、queueに入ったにも関わらずcontinueで無視されるものがあると一瞬想像してたものと違ってイメージしにくくなりました。

Copy link
Owner Author

Choose a reason for hiding this comment

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

ありがとうございます。

同意です。

同じロジックを2回書きたくないなと思いpop時にvalidateするようにしましたが、ご指摘のとおりだと思います。やはりNone判定はpush時にしたほうが大体の場合わかりやすいですね。

inorder_value_to_index[value] = i

dummy = TreeNode()
subtrees = deque() # first_index, last_index, parent, is_left
Copy link

Choose a reason for hiding this comment

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

どちらでもいいような気もしますが、stackとしてしか使わないのでただのリストでもいいかもと思いました。

subtrees.append((0, len(inorder) - 1, dummy, True))
preorder_index = 0
while subtrees:
inorder_first, inorder_last, parent, is_left = subtrees.pop()
Copy link

Choose a reason for hiding this comment

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

4つも引数があると関数の方がいいのではという気がします。ただ、どうせそうするならleft, rightの順番で関数を呼び出してpreorderの順番にする方法のがいいかもしれません。

# go left
stack[-1].left = node
stack.append(node)
else:
Copy link

Choose a reason for hiding this comment

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

ここで continue したほうが、ネストが浅くなり、読みやすくなると思いました。

Copy link
Owner Author

Choose a reason for hiding this comment

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

ありがとうございます。

たしかに、同意です。

inorder_value_to_index[value] = i

def build_subtree(pre_first, pre_last, in_first, in_last):
if pre_first > pre_last:
Copy link

Choose a reason for hiding this comment

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

自分なら

if pre_first == pre_last:
    return TreeNode(val=root_value)

と書きますが、好みの問題だと思います。 preorder が空の場合に動かなくなりますが、 buildTree() のほうで弾くと思います。

Copy link
Owner Author

Choose a reason for hiding this comment

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

ありがとうございます。

たしかに、length = 1は必ず通るので、そうすれば再帰の回数を概ね半減できそうで嬉しいですね。

validateの位置も同意です。

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.

4 participants