-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproblem106.py
More file actions
71 lines (53 loc) · 1.43 KB
/
problem106.py
File metadata and controls
71 lines (53 loc) · 1.43 KB
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
'''
106. Construct Binary Tree from Inorder and Postorder Traversal
Medium
Given inorder and postorder traversal of a tree, construct the binary tree.
Note:
You may assume that duplicates do not exist in the tree.
For example, given
inorder = [9,3,15,20,7]
postorder = [9,15,7,20,3]
Return the following binary tree:
3
/ \
9 20
/ \
15 7
Solution:
The same as 105, the difference is pop from the tail.
'''
class TreeNode(object):
def __init__(self, x):
self.val = x
self.left = None
self.right = None
class Solution(object):
def buildTree(self, inorder, postorder):
if not postorder:
return None
if not inorder:
return None
ele = postorder.pop()
inOrderIndex = inorder.index(ele)
left = inorder[:inOrderIndex]
right = inorder[inOrderIndex+1:]
currentNode = TreeNode(ele)
currentNode.right = self.buildTree(right,postorder)
currentNode.left = self.buildTree(left,postorder)
return currentNode
if __name__ == '__main__':
s = Solution()
postorder = [9,3,15,20,7]
inorder = [9,15,7,20,3]
root = s.buildTree(postorder,inorder)
def traverse(root):
if root is None:
return
traverse(root.left)
print root.val,
traverse(root.right)
traverse(root)
#Open this folder and please post a brief bio of yourself.
#Feel free to include your background, interests, hobbies,
#and what you hope to gain from this course.
#Think of this as your first day of class and get to know each other!