-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproblem105.py
More file actions
87 lines (71 loc) · 2.09 KB
/
problem105.py
File metadata and controls
87 lines (71 loc) · 2.09 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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
'''
105. Construct Binary Tree from Preorder and Inorder Traversal
Medium
Given preorder and inorder traversal of a tree, construct the binary tree.
Note:
You may assume that duplicates do not exist in the tree.
For example, given
preorder = [3,9,20,15,7]
inorder = [9,3,15,20,7]
Return the following binary tree:
3
/ \
9 20
/ \
15 7
Solution:
Preorder's first element is the organized root, divide the inorder list by the root,
the left part is the left subtree, right part is the right subtree.
Every time just pop the first element of the preorder list, and then start building the
left subtree, then right subtree.
Since the procedure is recursive, every node on the left subtree would be popped out
from the preorder list and formed a subtree, then it comes to right tree.
Which means we do not have to worry about whether the current root is in the inorder
sequence, cause we go over all the nodes and popped the nodes that do not belongs to
current inorder list.
Build the two children seperately.
'''
class TreeNode(object):
def __init__(self, x):
self.val = x
self.left = None
self.right = None
class Solution(object):
def buildTree(self, preorder, inorder):
if not preorder:
return None
if not inorder:
return None
ele = preorder.pop(0)
inOrderIndex = inorder.index(ele)
left = inorder[:inOrderIndex]
right = inorder[inOrderIndex+1:]
currentNode = TreeNode(ele)
currentNode.left = self.buildTree(preorder,left)
currentNode.right = self.buildTree(preorder,right)
return currentNode
"""
def buildTree(self, preorder, inorder):
if not preorder:
return None
if not inorder:
return None
ele = preorder.pop(0)
root = TreeNode(ele)
idx = inorder.index(ele)
root.left = self.buildTree(preorder, inorder[:idx])
root.right = self.buildTree(preorder, inorder[idx+1:])
return root
"""
if __name__ == '__main__':
s = Solution()
preorder = [1,2,4,5,6,7,3]
inorder = [4,2,6,5,7,1,3]
root = s.buildTree(preorder,inorder)
def traverse(root):
if root is None:
return
traverse(root.left)
print root.val,
traverse(root.right)
traverse(root)