Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
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)
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):
Copy link

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
Owner Author

Choose a reason for hiding this comment

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

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

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)
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]

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の値ということを読み取るのは難しいと思います。

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))