-
Notifications
You must be signed in to change notification settings - Fork 19
/
answer.py
32 lines (24 loc) · 878 Bytes
/
answer.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#!/usr/bin/python3
#------------------------------------------------------------------------------
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None
class Solution:
def buildTree(self, inorder, postorder):
"""
:type inorder: List[int]
:type postorder: List[int]
:rtype: TreeNode
"""
if len(postorder) == 0 or len(inorder) == 0:
return None
tree = TreeNode(postorder[-1])
j = inorder.index(postorder[-1])
tree.right = self.buildTree(inorder[j+1:], postorder[j:-1])
tree.left = self.buildTree(inorder[:j], postorder[:j])
return tree
#------------------------------------------------------------------------------
#Testing