【Arai60】29問目 105. Construct Binary Tree from Preorder and Inorder Traversal#29
Merged
shining-ai merged 3 commits intomainfrom Jun 29, 2024
Merged
【Arai60】29問目 105. Construct Binary Tree from Preorder and Inorder Traversal#29shining-ai merged 3 commits intomainfrom
shining-ai merged 3 commits intomainfrom
Conversation
nodchip
reviewed
Mar 20, 2024
|
|
||
| current_preorder_index = 0 | ||
|
|
||
| def array_to_tree(inorder_left, inorder_right): |
There was a problem hiding this comment.
区間は [inorder_left, inorder_right) の開閉区間で持たせたほうが一般的だと思います。
Owner
Author
There was a problem hiding this comment.
自分の分かりやすい方を主観で決めて実装していました。
開閉区間で統一するようにします。
| ) -> Optional[TreeNode]: | ||
| current_preorder_index = 0 | ||
|
|
||
| def array_to_tree(splied_inorder): |
There was a problem hiding this comment.
splied という単語が辞書で見つかりませんでした。綴りをご確認いただいてもよろしいでしょうか?
hayashi-ay
reviewed
Mar 22, 2024
hayashi-ay
left a comment
There was a problem hiding this comment.
良いと思います。別途preorder_indexを管理しない方法でも書いてみても良いかなと思います。
| nonlocal current_preorder_index | ||
| if inorder_left >= inorder_right: | ||
| return None | ||
| val = preorder[current_preorder_index] |
There was a problem hiding this comment.
valだけだとこれは何の値を意味するのかがわからないかなと思います。どこまで変数名として表現するかは難しいですが、valだけでは、[inorder_left, inorder_right)の区間で表されるTreeのrootの値ということを読み取るのは難しいと思います。
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
問題
https://leetcode.com/problems/construct-binary-tree-from-preorder-and-inorder-traversal/