-
Notifications
You must be signed in to change notification settings - Fork 0
【Arai60】29問目 105. Construct Binary Tree from Preorder and Inorder Traversal #29
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+92
−0
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
21 changes: 21 additions & 0 deletions
21
...9_Tree_BT_BST/29_105_Construct Binary Tree from Preorder and Inorder Traversal/level_1.py
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
class Solution: | ||
def buildTree( | ||
self, preorder: List[int], inorder: List[int] | ||
) -> Optional[TreeNode]: | ||
current_preorder_index = 0 | ||
|
||
def array_to_tree(split_inorder): | ||
nonlocal current_preorder_index | ||
if current_preorder_index >= len(preorder): | ||
return None | ||
val = preorder[current_preorder_index] | ||
if not val in split_inorder: | ||
return None | ||
current_preorder_index += 1 | ||
node = TreeNode(val) | ||
inorder_index = split_inorder.index(val) | ||
node.left = array_to_tree(split_inorder[:inorder_index]) | ||
node.right = array_to_tree(split_inorder[inorder_index + 1 :]) | ||
return node | ||
|
||
return array_to_tree(inorder) |
24 changes: 24 additions & 0 deletions
24
...9_Tree_BT_BST/29_105_Construct Binary Tree from Preorder and Inorder Traversal/level_2.py
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
# 再帰関数の引数をinorderのindexで管理 | ||
class Solution: | ||
def buildTree( | ||
self, preorder: List[int], inorder: List[int] | ||
) -> Optional[TreeNode]: | ||
inorder_index_map = {} | ||
for i, val in enumerate(inorder): | ||
inorder_index_map[val] = i | ||
|
||
current_preorder_index = 0 | ||
|
||
def array_to_tree(inorder_left, inorder_right): | ||
nonlocal current_preorder_index | ||
if inorder_left > inorder_right: | ||
return None | ||
val = preorder[current_preorder_index] | ||
node = TreeNode(val) | ||
inorder_index = inorder_index_map[val] | ||
current_preorder_index += 1 | ||
node.left = array_to_tree(inorder_left, inorder_index - 1) | ||
node.right = array_to_tree(inorder_index + 1, inorder_right) | ||
return node | ||
|
||
return array_to_tree(0, len(inorder) - 1) |
23 changes: 23 additions & 0 deletions
23
...9_Tree_BT_BST/29_105_Construct Binary Tree from Preorder and Inorder Traversal/level_3.py
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
class Solution: | ||
def buildTree( | ||
self, preorder: List[int], inorder: List[int] | ||
) -> Optional[TreeNode]: | ||
inorder_index_map = {} | ||
for i, val in enumerate(inorder): | ||
inorder_index_map[val] = i | ||
|
||
current_preorder_index = 0 | ||
|
||
def array_to_tree(inorder_left, inorder_right): | ||
nonlocal current_preorder_index | ||
if inorder_left > inorder_right: | ||
return None | ||
val = preorder[current_preorder_index] | ||
node = TreeNode(val) | ||
inorder_index = inorder_index_map[val] | ||
current_preorder_index += 1 | ||
node.left = array_to_tree(inorder_left, inorder_index - 1) | ||
node.right = array_to_tree(inorder_index + 1, inorder_right) | ||
return node | ||
|
||
return array_to_tree(0, len(inorder) - 1) |
24 changes: 24 additions & 0 deletions
24
...9_Tree_BT_BST/29_105_Construct Binary Tree from Preorder and Inorder Traversal/level_4.py
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
# [inorder_left, inorder_right) 右側を開区間に修正 | ||
class Solution: | ||
def buildTree( | ||
self, preorder: List[int], inorder: List[int] | ||
) -> Optional[TreeNode]: | ||
inorder_index_map = {} | ||
for i, val in enumerate(inorder): | ||
inorder_index_map[val] = i | ||
|
||
current_preorder_index = 0 | ||
|
||
def array_to_tree(inorder_left, inorder_right): | ||
nonlocal current_preorder_index | ||
if inorder_left >= inorder_right: | ||
return None | ||
val = preorder[current_preorder_index] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. valだけだとこれは何の値を意味するのかがわからないかなと思います。どこまで変数名として表現するかは難しいですが、valだけでは、[inorder_left, inorder_right)の区間で表されるTreeのrootの値ということを読み取るのは難しいと思います。 |
||
node = TreeNode(val) | ||
inorder_index = inorder_index_map[val] | ||
current_preorder_index += 1 | ||
node.left = array_to_tree(inorder_left, inorder_index) | ||
node.right = array_to_tree(inorder_index + 1, inorder_right) | ||
return node | ||
|
||
return array_to_tree(0, len(inorder)) |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
区間は [inorder_left, inorder_right) の開閉区間で持たせたほうが一般的だと思います。
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
自分の分かりやすい方を主観で決めて実装していました。
開閉区間で統一するようにします。