MCPcopy Create free account
hub / github.com/HuberTRoy/leetCode / Solution

Class Solution

Tree/ConstructBinaryTreeFromInorderAndPostorderTraversal.py:64–92  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

62# self.right = None
63
64class Solution(object):
65 def buildTree(self, inorder, postorder):
66 """
67 :type inorder: List[int]
68 :type postorder: List[int]
69 :rtype: TreeNode
70 """
71
72 def makeTree(inorder,
73 postorder):
74 if not inorder or not postorder:
75 return None
76
77 root = TreeNode(postorder.pop())
78 index = inorder.index(root.val)
79
80 # left_inorder = inorder[:inorder.index(root.val)]
81 # left_postorder = postorder[:len(left_inorder)]
82
83 # right_inorder = inorder[len(left_inorder)+1:]
84 # right_postorder = postorder[len(left_postorder):-1]
85
86
87 root.right = makeTree(inorder[index+1:], postorder)
88 root.left = makeTree(inorder[:index], postorder)
89
90 return root
91
92 return makeTree(inorder, postorder)

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected