Skip to content

【Arai60】29問目 105. Construct Binary Tree from Preorder and Inorder Traversal#29

Merged
shining-ai merged 3 commits intomainfrom
review29
Jun 29, 2024
Merged

【Arai60】29問目 105. Construct Binary Tree from Preorder and Inorder Traversal#29
shining-ai merged 3 commits intomainfrom
review29

Conversation

@shining-ai
Copy link
Copy Markdown
Owner


current_preorder_index = 0

def array_to_tree(inorder_left, inorder_right):
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

区間は [inorder_left, inorder_right) の開閉区間で持たせたほうが一般的だと思います。

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

自分の分かりやすい方を主観で決めて実装していました。
開閉区間で統一するようにします。

) -> Optional[TreeNode]:
current_preorder_index = 0

def array_to_tree(splied_inorder):
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

splied という単語が辞書で見つかりませんでした。綴りをご確認いただいてもよろしいでしょうか?

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

splitのtypoでした。

Copy link
Copy Markdown

@hayashi-ay hayashi-ay left a comment

Choose a reason for hiding this comment

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

良いと思います。別途preorder_indexを管理しない方法でも書いてみても良いかなと思います。

nonlocal current_preorder_index
if inorder_left >= inorder_right:
return None
val = preorder[current_preorder_index]
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

valだけだとこれは何の値を意味するのかがわからないかなと思います。どこまで変数名として表現するかは難しいですが、valだけでは、[inorder_left, inorder_right)の区間で表されるTreeのrootの値ということを読み取るのは難しいと思います。

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