Skip to content

Conversation

hayashi-ay
Copy link
Owner

@shining-ai
Copy link

変数の命名も含め読みやすいコードだと思いました。
mapで高速化していて勉強になりました。

root_index_in_inorder = inorder.index(root_value)
num_of_left_node = root_index_in_inorder
root = TreeNode(root_value)
root.left = self.buildTree(preorder[1:1 + num_of_left_node], inorder[:root_index_in_inorder])
Copy link

Choose a reason for hiding this comment

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

細かい点となりますが、「変数 + 1」と「1 + 変数」は、「変数 + 1」 に統一したほうが読みやすいかなと思いました。

Copy link
Owner Author

Choose a reason for hiding this comment

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

「インデックスの位置 + ずらす個数」という役割で順番を決めていましたが、指摘された内容も確かにと思いました。

「1 + num_of_left_node」は、rootの位置の隣(1)からnum_of_left_node分
「inorder[root_index_in_inorder + 1:]」はroot_index_in_inorderの位置から1つ分


def build_tree_in_preorder(left, right):
nonlocal root_index
if left >= right:
Copy link

Choose a reason for hiding this comment

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

if left == right: で十分ではないでしょうか…?

Copy link
Owner Author

Choose a reason for hiding this comment

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

仰る通りですが、 if left == rightのみだとif left > rightのケースが考慮しなくて良いということを読み手が確認するひと手間が発生するかなと思いif left >= rightにしました。

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.

3 participants